Forum Moderators: open

Message Too Old, No Replies

Help with a javascript regexp syntax problem please.

         

nelsonm

5:25 am on Mar 20, 2011 (gmt 0)

10+ Year Member



Hi all,

I found a regexp for validating email addresses that seems to be fairly complete and is a more practical implementation of RFC 2822 at the bottom of the page at [regular-expressions.info ].

I stuck it into the JavaScript function below. It works with the RegExp constructor using the literal string, as long as backslashes are escaped. I think it's working correctly since when testing it, it conforms to the allowed syntax discribed in the syntax section of [en.wikipedia.org ]. However, can't seem to get it to work at all using the special regex syntax "var myregex = /regex/;".

Can anyone tell me why it's not working at all using the regex syntax?

I have pasted the function below with the none working syntax commented out...

function checkEmail(field,alerttxt) {
var emailAddressRegex = new RegExp("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+(?:[A-Z]{2}|aero|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel)$");
//var emailAddressRegex = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|aero|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel)$/;

with(field){
if (value==null || value=='') {
alert(alertText);
return true;
}else{
if(!emailAddressRegex.test(value)) {
alert('A valid email address must be entered.');
return true;
}else{
return false;
}
}
}
}

Fotiman

9:51 pm on Mar 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Both syntaxes seem to work fine for me. Here's my test case:

<html>
<head>
</head>
<body>
<script>
function checkEmail(field,alerttxt) {
var emailAddressRegex = new RegExp("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+(?:[A-Z]{2}|aero|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel)$");
var emailAddressRegexLiteral = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|aero|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel)$/;
var result1, result2;
with (field) {
if (value == null || value == '') {
alert(alertText);
return true;
} else {
result1 = emailAddressRegex.test(value);
result2 = emailAddressRegexLiteral.test(value);
alert("Results were:\n" + result1 + "\n" + result2);
}
}
}
checkEmail({value:"fotiman@example.com"}, "");
checkEmail({value:"fotimanexamplecom"}, "");
</script>
</body>
</html>

nelsonm

1:07 am on Mar 22, 2011 (gmt 0)

10+ Year Member



Maybe i was getting confused in that the second var was altering the Dreamweaver code color scheme of the lines below it and i thought that was indicating a syntax error although Dreamweaver was not indicating any syntax error in the status bar.

All other regex literals i've coded in Dreamweaver appear green between the forward slashes. I thought this one was not working because maybe the forward slash's in the literal where interfering with the forward slash prefix and suffix.

I guess i jumped the gun. I'll try running the code.

thanks.

nelsonm

3:17 am on Mar 22, 2011 (gmt 0)

10+ Year Member



I think when using the literal syntax with the forward slash delimiters, any forward slashes in the string have to be escaped with a backward slash - correct?

ie.
/^[a-z0-9!#$%&'*+/=?^_`{|}~-]$/
should be
/^[a-z0-9!#$%&'*+\/=?^_`{|}~-]$/