Javascript email validation via regex

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Moriarty
Forum Newbie
Posts: 8
Joined: Tue Aug 22, 2006 9:33 am
Location: London, England. Home of Dangermouse.

Javascript email validation via regex

Post by Moriarty »

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?

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>
Thanks in advance,

Matt
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You have a string, not a regular expression. This validation should be done server-side. Just perform a simple check (if any) on the client-side.
Post Reply