Javascript email validation via regex
Posted: Fri Nov 10, 2006 10:14 am
I'm trying to do something simple but being my first whack at it, Im not finding it plain sailing.
I'd like to validate an email address via javascript using a regular expression. The expression is something I pinched from a search and it doesn't seem to work.
Could anyone give me an idea as to why I'm going wrong?
Thanks in advance,
Matt
I'd like to validate an email address via javascript using a regular expression. The expression is something I pinched from a search and it doesn't seem to work.
Could anyone give me an idea as to why I'm going wrong?
Code: Select all
<html>
<head>
<script>
function fncTestRegex()
{
MyRegex="/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i";
MyString="matt@waferthinmint.com";
alert("Regex="+MyRegex+"\rTest="+MyString);
if(!MyString.match(MyRegex)){alert('Pattern returned with a false')}
}
</script>
</head>
<body>
<input type="button" value="go for it" onclick="fncTestRegex()">
</body>
</html>Matt