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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript to validate e.mail 1

Status
Not open for further replies.

LaPluma

Programmer
Feb 3, 2002
139
DE
Hello

I am using a VBScript to try to validate the information input in a form. This is the code:

<%

'Function to Validate Email Address
Function ValidateEmail(email)
dim atCnt
ValidateEmail = false

if len(cstr(email)) < 7 then
ValidateEmail = true

elseif instr(email,&quot;@&quot;) = 0 then
ValidateEmail = true

elseif instr(email,&quot;.&quot;) = 0 then
ValidateEmail = true

elseif len(email) - instrrev(email,&quot;.&quot;) > 3 then
ValidateEmail = true

elseif instr(email,&quot;_&quot;) <> 0 and _
instrrev(email,&quot;_&quot;) > instrrev(email,&quot;@&quot;) then
ValidateEmail = true

else
atCnt = 0
for i = 1 to len(email)
if mid(email,i,1) = &quot;@&quot; then
atCnt = atCnt + 1
end if
next

if atCnt > 1 then
ValidateEmail = true
end if

for i = 1 to len(email)
if not isnumeric(mid(email,i,1)) and _
(lcase(mid(email,i,1)) < &quot;a&quot; or _
lcase(mid(email,i,1)) > &quot;z&quot;) and _
mid(email,i,1) <> &quot;_&quot; and _
mid(email,i,1) <> &quot;.&quot; and _
mid(email,i,1) <> &quot;@&quot; and _
mid(email,i,1) <> &quot;-&quot; then
ValidateEmail = true
end if
next
end if
End Function

'Usage of the Function
if ValidateEmail(request.form(&quot;email&quot;)) then
Reponse.write &quot;Invalid Email Address&quot;
else
Response.write &quot;Valid Email Address&quot;
End if

%>

The code refers to a form which has the following properties:

<align=center><table border=0 cellspacing=0 cellpadding=0 width=&quot;350&quot;>
<tr><td align=&quot;center&quot; valign=&quot;top&quot; width=&quot;350&quot;>
<form name=&quot;welcome&quot; method=&quot;post&quot; action=&quot;welcome1.asp&quot;>
</p>

<table bgcolor=&quot;#FF9900&quot;>


<tr><td align=&quot;center&quot;><b><font face=&quot;courier&quot; color=&quot;#FFFFFF&quot; size=-2>Name</font></b>
</td>

<td align=&quot;center&quot;><input type=&quot;text&quot; name=&quot;user_name&quot; size=12></td>
</tr>

<tr>
<td align=&quot;center&quot;><b><font face=&quot;courier&quot; color=&quot;#FFFFFF&quot; size=-1>E-mail</font></b>
</td>

<td align=&quot;center&quot;><input name=&quot;email&quot; type=text size=12><TYPE=HIDDEN NAME=&quot;RECIPIENT&quot; VALUE=&quot;info@grafik1.net&quot;></td>
</tr>

<tr><br>
<td align=&quot;center&quot;><b><font face=&quot;courier&quot; color=&quot;#FFFFFF&quot; size=-1>Message</font></b>
</td></tr>
</table>

<table><tr>
<td align=&quot;center&quot;><textarea name=&quot;message&quot; cols=&quot;20&quot; rows=&quot;3&quot;></textarea>
</td></tr>
</table>

In the above code, welcome1.asp (which works) refers to a 'Thank you + name' page which appears after the user has clicked on the submit button. For some reason, this still appears when I deliverately input a wrongly formatted e.mail address.

Is the problem something to do with the VBScript, or is it more likely to lie in the &quot;input name&quot; and/or &quot;input type&quot; values of the form itself?

I would be grateful for any suggestions.

Many thanks

LaPluma
 
I looked through the function and it seems fine along with some tests. The one thing that really stick sout is the email input tag.
you have the formatting like this
<input name=&quot;email&quot; type=text size=12>
it should be
<input type=text name=&quot;email&quot; size=12>
although this is trivial it may be causing a problem in the form passing the proper values. try the change and see if it helps out I dare to learn more
admin@onpntwebdesigns.com
 
Hello Onpnt

Many thanks for your message.

It looks more consistent the way you have it (the input box above the e.mail input box also uses <input type=&quot;text&quot; name=&quot;user_name&quot; size=12>, and I have copied and pasted it.

Unfortunately, it hasn't made any difference. However, I am pleased you think the validation code is OK, because I can try to narrow the problem down now.

Many thanks.

LaPluma
 
the main problem is that the name and thank you are displaying when a invalid email is being added but this is not what you want to happen right? I'm a little confused on the welcome1.asp refering to a page that displays this.
when the user enters the values and clicks submit the welcome1.asp is run and then are you redirecting to this other page? can you post the code for this other page that is displaying everything or am I really confused [lol]
I dare to learn more
admin@onpntwebdesigns.com
 
I really hate to see email and other form values like this getting validated server side jsut because it really is a waste of resources so if you want I redid this for you a bit to validate the email before the form ever gets submitted. It will keep you from having problems outside of this page then. Don't use it if you don't want too I just wanted to give you the suggestion. If anything give it a try and see what you think
everything I added to this is bolded
<html>
<head>
<script language=&quot;javascript&quot;>
function ValidateEmail(email) {
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/; if (!(filter.test(email))) {
alert('Invalid Email Address. Please re-enter your email again!');
return false;
} else {
return true;
}
}
</script>

</head>

<body>
<align=center><table border=0 cellspacing=0 cellpadding=0 width=&quot;350&quot;>
<tr>
<td align=&quot;center&quot; valign=&quot;top&quot; width=&quot;350&quot;>
<form name=&quot;welcome&quot; method=&quot;post&quot; action=&quot;welcome1.asp&quot; onSubmit=&quot;return ValidateEmail(document.welcome.email.value)&quot;>
</p>

<table bgcolor=&quot;#FF9900&quot;>


<tr>
<td align=&quot;center&quot;><b><font face=&quot;courier&quot; color=&quot;#FFFFFF&quot; size=-2>Name</font></b>
</td>

<td align=&quot;center&quot;><input type=&quot;text&quot; name=&quot;user_name&quot; size=12>
</td>
</tr>

<tr>
<td align=&quot;center&quot;>
<b><font face=&quot;courier&quot; color=&quot;#FFFFFF&quot; size=-1>E-mail</font></b>
</td>

<td align=&quot;center&quot;>
<input type=text name=&quot;email&quot; size=12>
<TYPE=HIDDEN NAME=&quot;RECIPIENT&quot; VALUE=&quot;info@grafik1.net&quot;>
</td>
</tr>

<tr>
<br>
<td align=&quot;center&quot;>
<b><font face=&quot;courier&quot; color=&quot;#FFFFFF&quot; size=-1>Message</font></b>
</td>
</tr>

<tr><td><input type=&quot;submit&quot; value=&quot;submit&quot;></td></tr>
</table>
</body>
</html> I dare to learn more
admin@onpntwebdesigns.com
 
Hello Onpnt

I must admit I can't help smiling to myself in admiration!

After we last talked I realised that the following:

<a href=&quot;welcome1.asp&quot;
onClick=&quot;document.welcome.submit();
return false&quot;>Submit</a>&nbsp;

seemed to be overiding the e.mail validation code. I thought the validation code would have prevented the welcome1.asp page (the 'Thank you + name page) from appearing.

You code works a treat:
The page needs a tidy up, but the main thing is that the mechanism is now in place for me to build on.

Many thanks - you're a wonder!

LaPluma
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top