0

I am trying to write a function in Javascript to validate email address. Here is the function.

function validateEmailAddress() {   
    var patternForEmail = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$/;
    var regexPatternForEmail = new RegExp(patternForEmail, 'i');

    // Email address and the Confirm Email address values should match
    if ($('#txtEmail').val() != $('#txtConfirmEmail').val()) {
        $('#dvErrorMsg').html("Email addresses do not match.");
        $('#txtEmail').focus();
        return false;
    }
    else if (!regexPatternForEmail.test($('#txtEmail').val())) {
        $('#dvErrorMsg').html("Please enter a valid email address.");
        $('#txtEmail').focus();
        return false;
    }
    else
        return true;
}

The problem here is I am getting an error, 'Syntax error in regular expression' during RegExp object instantiation.

I tried debuggin in IE 11 and that's where i found the error. Could someone please suggest me a solution for this.

Screen shot taken while debugging: enter image description here

1

2 Answers 2

2

You don't need to create another regex variable using RegExp constructor. Just use only the below.

var patternForEmail = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$/i;

i at the last called case-insensitive modifier which helps to do a case-insensitive match.

Example:

> var patternForEmail = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$/i;
> patternForEmail.test('[email protected]')
true
> patternForEmail.test('@[email protected]')
false
Sign up to request clarification or add additional context in comments.

5 Comments

I am fairly new to this. If you don't mind could you please tell me how ?
check for the match using the above regex variable.
@Dinesh You used new RegExp(someRegExp,'i') but should have used new RegExp(someString,'i') if anything (or just omit that line, as suggested). Using the RegExp constructor with a string requires further formatting the above pattern but because it’s a RegExp already it’s not necessary. So just remove that new RegExp line.
Also, the Firefox console gives me a different error: TypeError: can't supply flags when constructing one RegExp from another. You could actually remove that 'i' flag in the constructor. Note: if you remove the new line you have to append an i after the last / in the above line!
@AvinashRaj - Thanks for the suggestion. I dont see any errors now and things are working like charm.
0

In most of the times this kinds of errors occurs because of browser compatibility , so always we need to use the codes which can be run in all the browsers. I hope the following changes will help you.

var patternForEmail = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

then you can match the expression using

if (patternForEmail.test($('#txtEmail').val())) {
            $('#dvErrorMsg').html("Please enter a valid email address.");
            $('#txtEmail').focus();
            return false;
}

or else you can use match() function also which will be more flexible for all the browsers.

var email = $('#txtEmail').val();

if (!email.match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i)) {
        $('#dvErrorMsg').html("Please enter a valid email address.");
        $('#txtEmail').focus();
        return false;
}

2 Comments

Why is match more flexible than test?
it is on the case of compatibility and accuracy. Truthfully test() is faster than match(), but in accuracy match() is good. Because if the repeated test statements are created for the same value test() may fail, but match() still give correct answer , Regexp keeps track of the lastIndex when a new match is found.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.