Problem with email validation

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Problem with email validation

Post by Kingo »

Hello,
I'm having a problem with the email validation. If any one could suggest a solution..it would be great--------The text box is getting validated for an empty string but it is not for a Incorrect Email ID

<html>
<head>
<title>Valid</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript" type="text/javascript">

<!-- hide script from older browsers
function validateForm(contact1)
{

if(document.forms.contact1.Nam.value=="")
{
alert("Please enter your full name.");
document.forms.contact1.Nam.focus();
return false;
}

if(document.forms.contact1.Phon.value=="")
{
alert("Please enter your Phone NUmber.");
document.forms.contact1.Phon.focus();
return false;
}

if(document.forms.contact1.Emai.value=="")
{
alert("Please enter your Email ID.");
document.forms.contact1.Emai.focus();
return false;
}
if (document.forms.contact1.Emai.value!=("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})"))
{
alert("Please enter Correct Email ID.");
document.forms.contact1.Emai.focus();
return false;
}

}
stop hiding script -->
</script>
</head>
<body ><form action="form1.php" method="POST" name="contact1" onSubmit="return validateForm(contact1);">

<p>&nbsp;</p>
<p align="center"><strong><font color="#FF0000" size="2" face="Arial, Helvetica, sans-serif">Enter
Mailing List Information</font></strong></p>
<table width="34%" border="0" align="center">
<tr>
<td width="38%"><strong><font size="2" face="Arial, Helvetica, sans-serif">Name:</font></strong></td>
<td width="62%"><input type="text" name="Nam"></td>
</tr>
<tr>
<td><strong><font size="2" face="Arial, Helvetica, sans-serif">Phone:</font></strong></td>
<td><input type="text" name="Phon"></td>
</tr>
<tr>
<td><strong><font size="2" face="Arial, Helvetica, sans-serif">Email:</font></strong></td>
<td><input type="text" name="Emai"></td>
</tr>
<tr>
<td><div align="center">
<p>&nbsp;</p><p><input name="Submit" type="submit" id="Submit" value="Submit">
</p></div></td>
<td><div align="center">
<p>&nbsp;</p>
<p>
<input type="reset" name="Reset" value="Reset">
</p>
</div></td>
</tr>
</table>
<p align="center">&nbsp;</p>
<p>&nbsp;</p>
</form>
</body>
</html>
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

Do you have a Javascript manual? try looking up match()
the email needs to match the pattern .. this looks incorrect

Code: Select all

(document.forms.contact1.Emai.value!=("^&#1111;_a-z0-9-]+(\.&#1111;_a-z0-9]+)*@&#1111;a-z0-9-]+(\.&#1111;a-z0-9-]+)*(\.&#1111;a-z]&#123;2,3&#125;)"))
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

yes do it like

if(!document.forms.contact1.Emai.value.match("^[_a-z0-9-]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})"))){
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Post by Kingo »

Thanx very much....It is working
Post Reply