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!

Email Validate Filter 1

Status
Not open for further replies.

OldWilly

Programmer
Mar 1, 2002
66
What do I need to change in the below filter to make a dash (-) valid on the left side of the address?
Ex: john-smith@xxx.com

function FormValidator(theForm)
{
if (theForm.email.value == "")
{
alert("Please enter your \"Email Address\".");
theForm.email.focus();
return (false);
}
str=theForm.email.value;
filter=/^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str)!=true)
{
alert("Please input a valid email address!");
theForm.email.focus();
return (false);
}

return (true);
}
 
I currently use
Code:
function validEmail(str) 
{ 
  //valid an email address using regular expression
  var re=/^[A-Za-z0-9_\-]+([.][A-Za-z0-9_\-]+)*[@][A-Za-z0-9_\-]+([.][A-Za-z0-9_\-]+)+$/;
  return re.test(str);
}
Greg.
 
to make that a lot shorter you could cut your code down to this:
Code:
function validEmail(str) 
{ 
  //valid an email address using regular expression
  var re=/^[\w\-]+([.][\w\-]+)*[@][\w\-]+([.][\w\-]+)+$/;
  return re.test(str);
}

-kaht

banghead.gif
 
First:
if (theForm.email.value == "")
will allow all spaces

so i changed it to
if(theForm.email.value.split(" ").join("") == "")
to take care of them

Code:
<html>
<head>
<title></title>
<script type="text/javascript">
<!--
function FormValidator(theForm)
{
  if (theForm.email.value.split(" ").join("") == "") // Check for continuous spaces
  {
    alert("Please enter your \"Email Address\".");
    theForm.email.focus();
    return (false);
  }
   str=theForm.email.value;
   filter=/^((?:(?:(?:(\w|~)[~\.\-\+\/]?)*)(\w|~))+)\@((?:(?:(?:\w[\.\-\+]?){0,62})\w)+)\.([a-zA-Z]{2,6})$|^$/;
   if (!filter.test(str))
   {
   alert("Please input a valid email address!");
   theForm.email.focus();
   return (false);
  }

  return (true);
}
// -->
</script>
</head>
<body>
<form name="form1" method="post" action="" onsubmit="return FormValidator(this);">
<input type="text" name="email">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top