
Email validation function
I have this little function that validates an email address, but I cant seem to figure out how to get it to allow a totally empty entry. Its for an addressbook, and though I want all email addresses to be valid, I don't want them to be required.<BR>Here is the code:<BR><BR>Function IsValidEmail(Email)<BR>ValidFlag = False<BR>If (Email <> "") And (InStr(1, Email, "@") > 0) And (InStr(1, Email, ".") > 0) Then<BR>atCount = 0<BR>SpecialFlag = False<BR>For atLoop = 1 To Len(Email)<BR>atChr = Mid(Email, atLoop, 1)<BR>If atChr = "@" Then atCount = atCount + 1<BR>If (atChr >= Chr(32)) And (atChr <= Chr(44)) Then SpecialFlag = True<BR>If (atChr = Chr(47)) Or (atChr = Chr(96)) Or (atChr >= Chr(123)) Then SpecialFlag = True<BR>If (atChr >= Chr(58)) And (atChr <= Chr(63)) Then SpecialFlag = True<BR>If (atChr >= Chr(91)) And (atChr <= Chr(94)) Then SpecialFlag = True<BR>Next<BR>If (atCount = 1) And (SpecialFlag = False) Then<BR>BadFlag = False<BR>tAry1 = Split(Email, "@")<BR>UserName = tAry1(0)<BR>DomainName = tAry1(1)<BR>If (UserName = "") Or (DomainName = "") Then BadFlag = True<BR>If Mid(DomainName, 1, 1) = "." then BadFlag = True<BR>If Mid(DomainName, Len(DomainName), 1) = "." then BadFlag = True<BR>ValidFlag = True<BR>End If<BR>End If<BR>If BadFlag = True Then ValidFlag = False<BR>IsValidEmail = ValidFlag<BR>End Function<BR><BR>Thanks in advance!<BR>zale86<BR>

RE: Email validation function
Ummm...why not just:<BR><BR>Function IsValidEmail(Email)<BR> If Email = "" Then <BR> IsValidEmail = True<BR> Exit Function<BR> End If<BR> ...<BR> Flag = False<BR><BR>???<BR><BR>

RE: Email validation function
I need to beg your patience in advance... but I don't understand your reply. Are you suggesting that I replace all of the code I show with what you provided? If not, then where are you suggesting I insert your code? I'm really thrown off by the line "...". Again I beg your pardon for being so clueless. I am quite interested in learning however, so THANKS!<BR>zale86

Sorry!
My fault...should have been clearer!<BR><BR>Just put those few lines at the start of your function.<BR><BR>All they do is check: If the EMail is blank, then return a TRUE response. Nothing more. <BR><BR>EXIT FUNCTION is just a way of saying "I've already got the function value I need, so quit now!".<BR><BR>Then *after* that IF...END IF block you put the rest of your code, as is! That's what the ... meant.<BR><BR>(Well, you can pull out the test for EMail <> "", since you *know* that is true or you wouldn't have gotten past the initial IF that we just added.)<BR><BR>Does that make more sense?<BR><BR>Or did I misinterpret your original question?<BR><BR>

Looking at that code more...
...it doesn't make lots of sense to me!<BR><BR>You go to a lot of trouble to look for *some* special characters, but then you ignore all the others! If, for example, somebody managed to put a TAB or a BELL or other character into the string, you wouldn't detect it!<BR><BR>Can I try recoding that for you?<BR><BR><%<BR>' put all the characters that *are* valid in an email address in here:<BR>CONST validEmailChars = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_."<BR><BR>Function isValidEmail( ByVal email )<BR> ' a blank value is legal<BR> If email = "" Then <BR> isValidEmail = True<BR> Exit Function <BR> End If<BR> ' convert to upper case just for testing<BR> email = UCase( email )<BR> ' now look for bad characters:<BR> For ch = 1 To Len(email)<BR> If InStr( validEmailChars, Mid(email,ch,1) ) = 0 Then<BR> ' if we find a character not in list of valid ones...<BR> isValidEmail = False<BR> Exit Function<BR> End If<BR> Next<BR> ' characters all valid...now what about format?<BR> temp1 = Split( email, "@" )<BR> If UBound( temp1 ) <> 1 Then<BR> ' too many or too few @ characters!<BR> isValidEmail = False<BR> Exit Function<BR> End If<BR> If Len(temp1(0)) < 3 OR Len(temp1(1)) < 4 Then<BR> ' size of pieces too small<BR> isValidEmail = False<BR> Exit Function<BR> End If<BR> temp2 = Split( temp1, "." )<BR> If UBound(temp2) = 0 Then<BR> ' too few . characters!<BR> isValidEmail = False<BR> Exit Function<BR> End If<BR> If Len(temp2(0)) < 2 OR Len(temp2(1)) < 2 Then<BR> ' size of pieces too small<BR> isValidEmail = False<BR> Exit Function<BR> End If<BR><BR> isValidEmail = True<BR>End Function<BR>%><BR><BR>Hmmm...not any shorter than yours, is it? Ehhh...okay, if you like yours, leave it alone.<BR><BR><BR>

RE: Looking at that code more...
I used your code and it seems to work perfectly. I like that you were thinking of it's functionality beyond the "usual" characters.<BR>Thank you again for being so helpful. I truely appreciate it.<BR>zale86
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

