« Previous entry | Next entry » Browse > Bad code

Skip to comments (15) Email validation
Posted by Niek on Oct 17 2005 @ 12:14  :: 2313 unique visits

Well, this is by far the worst way to check the validity of an email address I've ever seen.

CODE: VBNET
Function isEmail(ByRef Invalue)
  Dim valueStr
  valueStr = trim(CStr(Invalue)) 
  If (Not(detectedEmailBadCharacters(valueStr))) Then
    If (Not(detectDoubleDotInARow(valueStr))) Then 
      If (Not(detectDoubleAtSimbol(valueStr))) Then
        If (isUserNameOk(valueStr)) Then
          If (isExtensionOK(valueStr)) Then
            If (isHostNameOK(valueStr)) Then
              isEmail = True
            Else
              isEmail = False
            End If
          Else
            isEmail = False
          End If
        Else
          isEmail = False
        End If
      Else
        isEmail = False
      End If
    Else
      isEmail = False
    End If
  Else
    isEmail = False
  End If
End Function

Function detectedEmailBadCharacters(ByRef strInput)
  Dim bad_chars, foundOne, counter, strIn, i, ch, a, bad_char
  bad_chars = "!$%^""&*()+={[}]:;'~#<,>?/| "
  'string of Nasty characters to be checked for.
  foundOne = False
  counter = 5
  strIn = CStr(strInput)
  For i = 1 To Len(strIn)
    foundOne = False
    ch = mid(strIn,i,1)
    For a = 1 To Len(bad_chars)
      bad_char = mid(bad_chars,a,1)
      If(ch = bad_char)Then
        detectedEmailBadCharacters = True
        Exit Function
      End If   
    Next ' a
  Next ' i
  detectedEmailBadCharacters = False
End Function

Function isUserNameOk(strInput)
  Dim intIn
  intIn = InStr(strInput,"@")
  If intIn = 0 Then
    isUserNameOk = False
  Else   
    isUserNameOk = True
  End If
End Function

Function isHostNameOK(ByRef strInput)
  If InStr(strInput," ") <> 0 Then
    isHostNameOK = False
    Exit Function
  ElseIf InStr(strInput,"@") = 1 Then
    isHostNameOK = False
    Exit Function
  ElseIf InStr((Len(strInput)-1),strInput,"@") <> 0 Then
    isHostNameOK = False
    Exit Function
  ElseIf InStr((Len(strInput)-1),strInput,".") <> 0 Then
    isHostNameOK = False
    Exit Function
  ElseIf InStr((Len(strInput)-1),strInput,"_") <> 0 Then
    isHostNameOK = False
    Exit Function
  Else
    isHostNameOK = True
    Exit Function
  End If
End Function

Function detectDoubleDotInARow(strInput)
  Dim ch, last_is_a_dot, i
  ch = ""
  last_is_a_dot = False
  For i = 1 To Len(strInput)
    ch = mid(strInput,i,1)
    If((ch = ".") And (last_is_a_dot = False)) Then
      last_is_a_dot = True
    ElseIf ch <> "." Then 
      last_is_a_dot = False
    ElseIf((ch = ".") And (last_is_a_dot = True)) Then
      detectDoubleDotInARow = True
      Exit Function
    End If
  Next
  detectDoubleDotInARow = False
End Function

Function detectDoubleAtSimbol(strInput)
  Dim ch, there_is_a_at_simbol, i
  ch = ""
  there_is_a_at_simbol = False
  For i = 1 To Len(strInput)
    ch = mid(strInput,i,1) 
    If((ch = "@") And (there_is_a_at_simbol = False)) Then
      there_is_a_at_simbol = True
    ElseIf((ch = "@") And (there_is_a_at_simbol = True)) Then
      detectDoubleAtSimbol = True
      Exit Function
    End If   
  Next
  detectDoubleAtSimbol = False
End Function

Function isExtensionOK(strInput)
  Dim counter, there_is_a_dot, at_position, i, ch
  counter = 0
  there_is_a_dot = false
  at_position = InStr(1,strInput,"@")
  For i = (at_position + 1) To Len(strInput)
    ch = mid(strInput,i,1)
    counter = counter + 1
    If((ch = ".") And (counter = 1)) Then
      isExtensionOK = False
      Exit Function
    ElseIf((ch = ".") And (counter > 1)) Then
      isExtensionOK = True
      Exit Function
    End If
  Next
End Function


Borrowed from The Daily WTF.

15 comments posted so far
Add your own »

1. On Oct 18 2005 @ 03:35 dustin wrote:

hahaha wow that's insanity. Does it even work?

2. On Oct 19 2005 @ 15:59 guest wrote:

OMFG! Lol, that was a real lol to when i read that.  Having done a course of basic programming i know that whoever wrote this actually had a clue (indentation for nested loops/if statments, descriptive variables, etc) and theyve compiled that lump of code..

3. On Oct 19 2005 @ 18:50 badfire wrote:

nice

4. On Oct 19 2005 @ 19:45 guest wrote:

hmm, would be good if your going for project size and complexity

5. On Oct 20 2005 @ 02:05 guest wrote:

Holy crap!  Nurse, get this man 1cc of Regular Expressions, stat!

6. On Oct 20 2005 @ 03:09 knodi wrote:

If he's paid per line of code, this man is a f***ing genius.

7. On Oct 20 2005 @ 03:10 guest wrote:

indeed, this "man" is a genius.

8. On Oct 20 2005 @ 21:52 Dave wrote:

Wow. Could that be much worse  :)

9. On Oct 22 2005 @ 20:02 codecoaster wrote:

Reminds me a bit of this function I wrote after doing a 12 week course on JavaScript. I could have used a one line regular expression which anyone can download from the internet but as I hate the things I wrote this.

/********************************************************
 * Function:doEmail()                    *
 * Purpose : This function checks that an Email address  *
 * has been entered in the form. it checks for @ *
 * and ".". It makes sure that there is some  *
 * characters before @ and before ".". It also  *
 * ensures that the value after "." contains some*
 * characters but no more than four. As Email is *
 * an optional field it just alerts the customer *
 * if the eMail isn't quite right.    *
 * Input   : none          *
 * Output  : none      *
 * Author  : Rosemary Wood                               *
 * Date    : July 12th, 2005                             *
 * Notes   :This function was written by me to check that*
 * a valid eMail address is entered.             *
 * Limitations: It accepts non-alphanumeric chars*
 * as all Emails are accepted anyway this is not *
 * really a problem. Due to other commitments I  *
 * will have to leave it like this for the moment*
 * but it could be very easily changed to do this*
 ********************************************************/
function doEmail(e){
  var isE=false; //set local variable isE to false.
  var emCom=null;  //initialise local var emCom for holding end of eMail eg .com
  var eM=e.value; //assign value of billEmailAddress field to eM
  if(eM.indexOf("@")>=0){  //check that @ is in the address
     ema=eM.split("@");  //split string at @ and assign to ema
 if(ema[0].length>0){  //check that first position in ema array holds something    
    if(ema[1].indexOf(".")>=0){  //check that index[1] of ema has a dot in it
           emCom=ema[1].split(".");  //If it does then split at the dot and assign to emCom
       if(emCom[1].length<=4&&emCom[1].length>0){ //check that index[1] of emCom is greater than 0 and less than 4
      isE=true;   //if all these checks have been passed then set isE to true
       }
    }
     }
  }
if(!isE){   //if the eMail isnt valid
  alert("You have a very strange email address!"); //the customer is informed but not hassled
  document.buying.billEmailAddress.focus(); //focus could return to eMail address if all required fields completed
}
  //if isE isn't true alert customer that there's something strange about their eMail
}//this function only informs customer it doesn't change the eMail address or call doFocus because its not a required field

This was part of my final assessment and attached to a form. By the way I got a distinction lol.

10. On Oct 24 2005 @ 18:29 guest wrote:

codecoaster: If i were to use your script then
i.e.,

&&@&&.&&&
or even worst case,
@@@@@@.@@@

will be valid because it doesn't check for invalid characters or even anything except for
- if @ and . is present
- and the value after the . is > 0 and < 4

11. On Mar 17 2006 @ 13:39 kaos wrote:

damn, this is crazy
i viewed the first function
and i didn't even want to read the rest ones
use regular expressions would really help

12. On Apr 29 2009 @ 11:43 guest wrote:

When the wolf wow gold finally found the hole Buy Wow Gold in the chimney he Cheap WoW Gold crawled down and KERSPLASH right into that kettle of water cheapest wow gold and that was the end of his troubles with the big bad wolf.

game4power,buy cheap wow gold
WOW GOLD

The next day the wow gold cheap little pig invited his mother over . She said "You see it is just as buy gold wowI told you. The way to Wow Goldget along in the world is to do things as well as you can." Fortunately for that world of warcraft gold little pig, he Cheapest wow Goldlearned that lesson. And he just lived happily ever after!

14. On Jul 14 2009 @ 04:04 guest wrote:

buy wow gold
my wow power leveling
buy wow gold
good wow power leveling
BUY wow gold
my wow power leveling
CHEAP rs gold
cheap wow power leveling
CHEAPEST lotro gold
MY aion gold
buy wow gold
cheap wow gold
CHEAPEST wow gold

Add a new comment

Name:
Password: (leave empty for anonymous comment)
 
View formatting tags Comment: