Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate for TLD 1

Status
Not open for further replies.

gwillr

IS-IT--Management
Nov 4, 2003
267
CA
I currently have an email field validation that is okay, but I am looking to add in something that will verify that what has been entered ends in one of the TLDs (com, ca, info, biz, org and so on.)

I am unsure of how to implement that additional feature in to what I have:

Code:
'Email
        re.Pattern = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
        errorArray(2) = re.Test(xemail)
        if errorArray(2) then
            errorArray(2) = False
        else
            errorArray(2) = True
            ErrorMsg = ErrorMsg & "Email Address<br>"
        
        end if

Currently, the string only checks to see that the tld contains only a-z chars.

Thanks in advance for comments and help.

Gary

 
Do you really need it? It is extra maintenance because as new TLDs become available you need to know about them and then update your validation.

Just ensuring that the address structure is valid should be sufficient.

Tony
_______________________________________________________________
 
Well Tony, you raise a valid concern.

I guess I am a bit of a perfectionist, so it will nag at me if i neglect it.

Any Help at this point is appreciated, as I have spent two days researching!



Gary

 
Gary,

I used a static array to possibly get you in the right direction for handling the extensions. No error coding except to see if the extension compared to the array matches. If it does - submit it to database. Play around with error messages since i have none. Just quick and dirty ;-) Hope it helps you out.

BSL

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <meta HTTP-EQUIV="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
 <body>
  <form action="email.asp" method="POST">
   Email: <input name="email" size="30">
          <input type="submit" value="Submit">
  </form>

<%
  Dim tldArray, email
 
  email = Request.Form("email")

  If email <> "" Then
  
  tldArray = Array("ca","biz","com","gov", "mil", "info")

  For i = 0 to uBound(tldArray)

  formatEmail = Left(email,InStrRev(email,".") - 1) & "." & tldArray(i) 

  tldConfirmation = StrComp(formatEmail, email)

  If tldConfirmation = 0 Then tldVerified = 0
  
  Next  

   If tldVerified = 0 Then 

   ' add to database 

   End If 

   
  
  
  End If

  
%>

</body>
</html>
 
re.Pattern = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*(com|ca|info|biz|org)$"

the mode u wanna cover the mode u have to add to the end...

Known is handfull, Unknown is worldfull
 
Thanks everyone for the input.

bslintx - I have taken your advice and set something up similar, but still have a few bugs to work out, may p[ost them later if i cant get through them.

vbkris - I have tried using the string yo have suggested. it casuses nothing to be allowed through, even valid addresses. is the syntax correct for the last part?

Gary

 
thats strange, can i have your code after the change i suggested??? along with an example email id that you tried...

Known is handfull, Unknown is worldfull
 
vbkris

Thanks for the follow up response. I have been been playing with a few examples since this post, but here is teh code as it now sits from that original one we talked about: ( I have tried several emails, one, for example, that didnt work is name@domain.com )

Here is to code as it sits right now at
Code:
<%@ Language=VBScript %>

<%DIM ErrorMsg,xemail,xname%>
<%


	
	const numFields = 2
	dim errorArray() 
	redim preserve errorArray(numFields)

	if request.form("isSubmitted") = "yes" then
		xname = request.form("xname")
		xemail = request.form("xemail")
		
			xname = REPLACE(trim(request("xname")),"'","''")
			xemail = REPLACE(trim(request("xemail")),"'","''")


		ErrorMsg = ""
		dim re
		set re = New RegExp


		'First Name
		re.Pattern = "^[^0-9\/><\.,\\!\^\$\*\+\?@#%&\(\);:\[\]\{\}=""']+$"
		re.Global = True
		re.IgnoreCase = True
		errorArray(0) = re.Test(xname)
		if errorArray(0) then
		errorArray(0)=False
		else
			errorArray(0) = True
			ErrorMsg = "Please Fix First Name<br>"
		end if



'Email
		re.Pattern = "^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z].]*(com|ca|info|biz|org)$"
		errorArray(1) = re.Test(xemail)
		if errorArray(1) then
			errorArray(1) = False
		else
			errorArray(1) = True
			ErrorMsg = ErrorMsg & "Please Fix Email Address<br>"
		
		end if		
		
	end if	
	
%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- #INCLUDE FILE = "DataStore.inc" -->
<!-- #INCLUDE FILE = "adovbs.inc"    -->
<title>ASP Form Validation Sample</title>
<style type="text/css">
<!--
body {  font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #000000}
table {  font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #000000}
-->
</style>



</head>
<body bgcolor="#FFFFFF">
  
<%if ErrorMsg <> "" then %>
<font color="red" size="3"> <%= ErrorMsg %> </font> 
<%end if%>
<form name="sample1" method="post">
  <table width="50%" cellspacing="1" cellpadding="0" border="0">

	
	<tr>
		<td>
			<% if errorArray(0) = True then %><font color="red"><b><% end if %>Name: <% if errorArray(0) = True then %></b></font><% end if %>
		</td>
		<td>
			<input name="xname" value="<%= xname %>" size="20">
		</td>
	</tr>
	
	<tr>
		<td>
			<% if errorArray(1) = True then %><font color="red"><b><% end if %>Email Address: <% if errorArray(1) = True then %></b></font><% end if %>
		</td>
		<td>
			<input name="xemail" value="<%= xemail %>" size="20">
		</td>
	</tr>
	
	<tr>
		<td colspan="2">
			<input type="submit" value="Submit"><input type="hidden" name="isSubmitted" value="yes">
		</td>
	</tr>
	</table>
	</form>

	
<%if request.form("isSubmitted") = "yes" then



%>


<% 
DIM all,i
all=False
For i=0 to 1
all=all OR CBOOL(errorArray(i))

Next

If Not all Then

'create variable to hold email address
	  DIM EMail
	  EMail = Request.Form("xemail")
	  Dim Name
          Name = Request.Form("xname")

	'create variable to hold database object and initialize it
	  Dim objRec
	  Set objRec = Server.CreateObject("ADODB.Recordset")

	'open the database table asptest
	  objRec.Open "test", strConnect, adOpenStatic, _
  	    adLockOptimistic, adCmdTable
    
	'create a new record
	  objRec.AddNew
	'set the field equal to the variable
   	  objRec("EMail") = EMail
	  objRec("Name") = Name
	'update the database with the new record
	  objRec.Update

	'close the database
	  objRec.Close
	  Set objRec = Nothing



	'un-comment this line to point to your desired confirm page
	 Response.Redirect("[URL unfurl="true"]http://www.robarspages.ca/devroot/computersite/aspvalid2.asp")[/URL]

        xname = request.form("xname")

		xemail = request.form("xemail")
		

	end if
		end if
		
%>




<a href="[URL unfurl="true"]http://www.robarspages.ca/devroot/compUTERsite/list.asp">Check[/URL] the DB by clicking here.</A><br /><br />

</body>
</html>



I ahve been playing with another example as well, but have problems with it listing the correct error message, and highlighting the correct form lable if incorrect. Here is that code:


Code:
   <%@ Language=VBScript %>
<!-- #INCLUDE FILE = "DataStore.inc" -->
   <!-- #INCLUDE FILE = "adovbs.inc"	-->

   <%DIM ErrorMsg,xemail,xname%>
   <%
   
   'Error Messages
   Dim errorArray(3)
   errorArray(0) = "Record Updated Successfully"
   errorArray(1) = "Error In User Name"
   errorArray(2) = "Error In E-mail Address"
   
   errorCode = Request.QueryString("error")
   
   'If submit, then update DB
   if request.form("isSubmitted") = "yes" then
 	  
 	'Format Values
 	  xname = REPLACE(trim(request("xname")),"'","''")
 	  xemail = REPLACE(trim(request("xemail")),"'","''")
   
 	  'Check fields for valid characters
 	  dim re
 	  set re = New RegExp
 	  re.Global = True
 	  re.IgnoreCase = True
 	  
 	  'Name
 	  re.Pattern = "^[^0-9\/><\.,\\!\^\$\*\+\?@#%&\(\);:\[\]\{\}=""']+$"
 	  If re.Test(xname)=false Then
 		 Response.redirect "aspvalid4.asp?error=1&uname=" & xname & "&uemail=" & xemail
 		  Response.end
 	  end if
    
 	  'Email
 	 re.Pattern = "^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
 	  If re.Test(xemail)=false then
 		 Response.redirect "aspvalid4.asp?error=2&uname=" & xname & "&uemail=" & xemail
 		  Response.end
 	  end if
 	
   '=====================================TLD TEST CODE
 	
 	Dim emailArray
 	Dim TLDisValid
 	Dim ValidTLD(4)'modify this
 	
 	TLDisValid=false
 	
 	ValidTLD(0) = "com"
 	ValidTLD(1) = "us"
 	ValidTLD(2) = "net"
 	ValidTLD(3) = "ca"
 	
 	emailArray = split(xemail,".")
 	
 	For i = 0 to ubound(ValidTLD)
 	 If lcase(emailArray(ubound(emailArray))) = lcase(ValidTLD(i)) then
 	 TLDisValid=True
 	 end if
 	next
 	
 	If TLDisValid=False then
 		 Response.redirect "aspvalid4.asp?error=2&uname=" & xname & "&uemail=" & xemail
 		  Response.end
 	end if
   
    
   '========================================END TLD TEST CODE
 	 
    
    'create variable to hold database object and initialize it
 	 Dim objRec
 	 Set objRec = Server.CreateObject("ADODB.Recordset")
    'open the database table asptest
 	 objRec.Open "test", strConnect, adOpenStatic, _
 		 adLockOptimistic, adCmdTable
 	  
    'create a new record
 	 objRec.AddNew
    'set the field equal to the variable
 	 objRec("EMail") = xemail
 	 objRec("Name") = xname
    'update the database with the new record
 	 objRec.Update
    'close the database
 	 objRec.Close
 	 Set objRec = Nothing
 	 
 	 'Return Success
 	 Response.redirect "aspvalid4.asp?error=0&uname=&uemail="
 	  Response.end
  
 	
    end if 
    
   %>
    
   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
   <html>
   <head>
   <title>ASP Form Validation Sample</title>
   <style type="text/css">
   <!--
   body {  font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #000000}
   table {  font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #000000}
   -->
   </style>
    
   </head>
   <body bgcolor="#FFFFFF">
   <%'Display Error
   If len(errorCode)>0 then%>
   <font color="red" size="3"><%=errorArray(errorCode)%></font>
   <%end if 'Display Error%>
   <form name="sample1" method="post">
 	<table width="50%" cellspacing="1" cellpadding="0" border="0">
    
    <tr>
 	<td>
   <% If len(errorCode)=2 then %><font color="red"><b><% end if %>Name: <% If len(errorCode)=2 then %></b></font><% end if %>
 	</td>
 	<td>
 	 <input name="xname" value="<%=Request.QueryString("uname")%>" size="20">
 	</td>
    </tr>
    
    <tr>
 	<td>
 	     <% If len(errorCode)=1 then %><font color="red"><b><% end if %>Email Address: <% If len(errorCode)=1 then %></b></font><% end if %>
 	</td>
 	<td>
 	 <input name="xemail" value="<%=Request.QueryString("uemail")%>" size="20">
 	</td>
    </tr>
    
    <tr>
 	<td colspan="2">
 	 <input type="submit" value="Submit"><input type="hidden" name="isSubmitted" value="yes">
 	</td>
    </tr>
    </table>
    </form>
  <A href="[URL unfurl="true"]http://www.robarspages.ca/devroot/compUTERsite/list.asp">Check[/URL] the DB by clicking here.</A><br><br>
   </body>
   </html>

Thanks very much for any expertise! I appreciate it.

Gary

 
let me take a look at it...

Known is handfull, Unknown is worldfull
 
this worked for me:

<%
TheEmail="asd@asd.CO"
set re = New RegExp
re.ignorecase=true
re.global=true


re.Pattern = "^[a-z](?:[\-_]?[a-z0-9]+\.?)*?[a-z0-9]@[a-z0-9](?:[\-_]?[a-z0-9]+\.?)*?(com|edu)$"


if not (re.test(TheEmail)) then
response.write "Wrong Email"
else
response.write "Correct Email"
end if
%>

i have changed the RegExp a bit...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top