Server Side E-Mail Validation

The chkEmail function performs a thorough check of an entered email address. The following criteria is checked by chkEmail:

  • only one @ sign permitted
  • domain extension separator (.) must come after the @ symbol
  • must be at least 2 characters (letters) after the domain extension separator (.)
  • rejects all illegal characters including spaces.
  • Allows only numbers, letters, the underscore (_) and the dash (-) character as valid input (excluding the mandatory "@" and "." symbols).
  • Minimum of 5 characters
chkEmail returns True if an email address is validated successfully, otherwise False is returned. If an error is encountered when processing an email address, chkEmail returns False.

Syntax: boolean = chkEmail(mailaddress)

Example Usage

<%
' chkEmail returns true
response.write chkemail("me@mysite.com")
response.write chkEmail("me_1@mysi_te.com")
response.write chkEmail("101@mysite.ca")
response.write chkEmail("ag_wd@e.jp")

' chkEmail returns false
response.write chkEmail("steve@@d.com")
response.write chkEmail("sd.john@ccc.com")
response.write chkEmail("me@mysite.123")
response.write chkEmail("me@mysitecom")
response.write chkEmail("memysite.com")
response.write chkEmail("me@m][ysite.com")
response.write chkEmail("me@mysit&e.com")
%>

Function

<%
 function chkEmail(theAddress)
   ' checks for a vaild email
    ' returns 1 for invalid addresses
    ' returns 0 for valid addresses
    dim atCnt
    chkEmail = 0
 
   ' chk length
    if len(mcstr(theAddress)) < 5  then
       ' a@b.c should be the shortest an
       ' address could be
       chkEmail = 1
 
   ' chk format
    ' has at least one "@"
    elseif instr(theAddress,"@") = 0 then
       chkEmail = 1
 
   ' has at least one "."
    elseif instr(theAddress,".") = 0 then
       chkEmail = 1
 
   ' has no more than 3 chars after last "."
    elseif len(theAddress) - instrrev(theAddress,".") > 3 then
       chkEmail = 1
 
   ' has no "_" after the "@"
    elseif instr(theAddress,"_") <> 0 and _
        instrrev(theAddress,"_") > instrrev(theAddress,"@")  then
       chkEmail = 1
 
   else
       ' has only one "@"
       atCnt = 0
       for i = 1 to len(theAddress)
          if mid(theAddress,i,1) = "@" then
             atCnt = atCnt + 1
          end if
       next
 
      if atCnt > 1 then
          chkEmail = 1
       end if
 
      ' chk each char for validity
       for i = 1 to len(theAddress)
         if not isnumeric(mid(theAddress,i,1)) and _
 (lcase(mid(theAddress,i,1)) < "a" or _
 lcase(mid(theAddress,i,1)) > "z") and _
 mid(theAddress,i,1) <> "_" and _
 mid(theAddress,i,1) <> "." and _
 mid(theAddress,i,1) <> "@" and _
 mid(theAddress,i,1) <> "-" then
             chkEmail = 1
         end if
       next
   end if
 end function
 %>


ASP validation email forms ASP ADO VBScript DOM tutorial authoring programming howto reference examples samples source code demos tips


Back To Top
© 1998 - 2024 psacake.com
Version 7.21 | Advertise on this site