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

EMAIL validation doesn't work...

Status
Not open for further replies.

perryair

IS-IT--Management
Apr 7, 2005
91
IL
Hi...
I am having problem with my script, I just added the email validation script and it does not work with the standard
one...

please help...

<html dir="ltr">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255">
<title>Send email</title>

<script language="javascript" type="text/javascript">

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

if(""==document.forms.validation.fname.value)
{
alert("Please enter first name");
document.forms.validation.fname.focus();
return false;
}

}
stop hiding script -->
</script>

<script language="JavaScript1.2">

var testresults
function checkemail(){
var str=document.validation.emailcheck.value
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str))
testresults=true
else{
alert("Please input a valid email address!")
testresults=false
}
return (testresults)
}
</script>

<script>
function checkbae(){
if (document.layers||document.getElementById||document.all)
return checkemail()
else
return true
}
</script>

</head>

<body>
<form name="validation" onSubmit="return validateForm(); checkbae();">
&nbsp;<p>First Name:<br>
<input type="text" size=18 name="fname">&nbsp; </p>
<p>Please input a valid email address:<br>
<input type="text" size=18 name="emailcheck">
<input type="submit" value="Submit">
</p>
</form>

</body>

</html>
 
Change your validation script to something like this:

Code:
<script language="javascript" type="text/javascript">

function validateForm(myValidation)
{
if(myValidation.fname.value.length == 0)
{
alert("Please enter first name");
myValidation.fname.focus();
return false;
}

var str=myValidation.emailcheck.value
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str))
{
return true;
}
else
{
alert("Please input a valid email address!");
myValidation.emailcheck.focus();
return false;
}
}
</script>

Then change your form tag to this:

Code:
<form name="validation" onSubmit="return validateForm(this)">

Now you validate function will check both items at the same time and you'll reduce the amount of code needed to perform the validation.

There's always a better way. The fun is trying to find it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top