Email validation

Results 1 to 2 of 2

Thread: Email validation

  1. #1
    Join Date
    Dec 1969

    Default Email validation

    Bill Wilkinson solved my email validation problem by offering the following code. My question is this though: I want to have fields for three email addresses, not just one. Is there an easy way to make these validation functions work for three different addresses without having to repeat the entire code three times?<BR>Here is the code:<BR><BR>Function SpaceStr(StrToSpace, EveryNspaces)<BR> ssStr = "": CurrCount = 0<BR> For ssIndex = 1 To Len(StrToSpace)<BR> ssChr = Mid(StrToSpace, ssIndex, 1)<BR> If ssChr &#060;&#062; " " Then<BR> CurrCount = CurrCount + 1<BR> Else<BR> CurrCount = 0<BR> End If<BR> If CurrCount &#062;= EveryNspaces Then<BR> ssChr = " " & ssChr: CurrCount = 0<BR> End If<BR> ssStr = ssStr & ssChr<BR> Next<BR> SpaceStr = Trim(ssStr)<BR>End Function<BR><BR><BR> <BR>&#039; put all the characters that *are* valid in an email address in here: <BR>CONST validEmailChars = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-." <BR><BR>Function isValidEmail( ByVal email ) <BR>&#039; a blank value is legal <BR>If email = "" Then <BR>isValidEmail = True <BR>Exit Function <BR>End If <BR>&#039; convert to upper case just for testing <BR>email = UCase( email ) <BR>&#039; now look for bad characters: <BR>For ch = 1 To Len(email) <BR>If InStr( validEmailChars, Mid(email,ch,1) ) = 0 Then <BR>&#039; if we find a character not in list of valid ones... <BR>isValidEmail = False <BR>Exit Function <BR>End If <BR>Next <BR>&#039; characters all what about format? <BR>temp1 = Split( email, "@" ) <BR>If UBound( temp1 ) &#060;&#062; 1 Then <BR>&#039; too many or too few @ characters! <BR>isValidEmail = False <BR>Exit Function <BR>End If <BR>If Len(temp1(0)) &#060; 3 OR Len(temp1(1)) &#060; 4 Then <BR>&#039; 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>&#039; too few . characters! <BR>isValidEmail = False <BR>Exit Function <BR>End If <BR>If Len(temp2(0)) &#060; 2 OR Len(temp2(1)) &#060; 2 Then <BR>&#039; size of pieces too small <BR>isValidEmail = False <BR>Exit Function <BR>End If <BR><BR>isValidEmail = True <BR>End Function <BR><BR>Thanks in advance,<BR>Zale86

  2. #2
    Join Date
    Dec 1969

    Default RE: Email validation

    That&#039;s the beauty of functions - you don&#039;t have to repeat the code three times, just call the function three times.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts