ASP eMail validation script #1
Enter an eMail address:
Here is the code:
<% ' Checks to see if the user inputted a value ' If so, remove leading and trailing blanks and upcase it xemail= ucase(trim(request.form("xemail"))) twd_submit=trim(request.form("twd_submit")) ' Grab the length of the email address inputted email_len=len(xemail) ' if the user has inputted a value start checking it if trim(xemail) <> "" then ' Loop that will check each character of the inputted value ' for the @ and the dot for counter = 1 to email_len 'If there is an @ set twd_at to the position it was found in if mid(xemail,counter,1)="@" then ' count the number of @'s at_counter=at_counter+1 ' if there is more than one add it to the message if at_counter > 1 then message = message &"There appear to be multiple @'s in the email address<br>" end if ' if this is the 1st @ note the location in the string if twd_at = "" then twd_at=counter end if ' end check for first @ end if ' end check for the @ 'If there is an dot (.) set twd_dot to the position it was found in if mid(xemail,counter,1)="." then if twd_dot = "" then twd_dot=counter end if 'end check for the first dot end if 'end check for the dot next ' Check to see if the dot comes after the @ ' and that the first dot is not the last character if (twd_dot < twd_at) or (len(xemail) <= twd_dot+1) or ((twd_dot-twd_at) < 2) or (twd_at < 2)then message = message & "Email convention appears to be wrong <br>" end if 'end check for dot after the @ ' Scan the user input to see that all inputted values are either a letter A-Z, ' a number 0-9 or if the character is a . or and @. for counter=1 to len(xemail) if (mid(xemail,counter,1) <> "/") and ((mid(xemail,counter,1) > chr(45)) and (mid(xemail,counter,1) < chr(58))) or ((mid(xemail,counter,1) > chr(63)) and (mid(xemail,counter,1) < chr(91))) or (mid(xemail,counter,1) = chr(95)) then else ' If it's an invalid charcter add it to the display message message = message & "Invalid charcter "& mid(xemail,counter,1)& " found in email address <br>" end if 'end check for invalid characters next 'end loop for invalid characters end if 'end check for user input ' Check the email field for input if is blank ' then add to the display message if (xemail = "") and (twd_submit <> "") then message= message & "Please enter an email address<br>" end if ' If the email address is not OK than display the message(s) ' and show the text box for user input with the last value pre-filled if message <> "" or xemail = "" then response.write message end if %> <form action="<%= request.servervariables("script_name") %>" method="post"> <input type="text" name="xemail" value="<%= lcase(xemail) %>" size="30"> <input type="submit" name="submit"> <input type="hidden" name="twd_submit" value="Yes"> </form>