0

Why is my validation failing on second attempt here? On first attempt the validation works fine, but on the second run, it will accept inappropriate values in the email field. The first two fields work fine, but the third field will accept any text after the first run. However it only validates if I change the value in the email field, otherwise it will keep displaying the error and failing to validate like it should.

function validate(){

    clearErrors();

    var errorFlag = true;

    var name = document.getElementById("name");

    nameVal = name.value;

    if(nameVal.length === 0){

        var nameError = document.getElementById("nameError");

        nameError.style.display = "block";

        errorFlag = false;
    }

    var phone = document.getElementById("phone")

    var phoneVal = phone.value;

    if(isNaN(phoneVal) || (phoneVal < 1000000000 || phoneVal >10000000000)){

        errorFlag = false;

        var phoneError = document.getElementById("phoneError");

        phoneError.style.display = "block";
    }



    var email = document.getElementById("email");

    var emailVal = email.value;

    var reStr = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$";

    if((reStr.match(emailVal))){

        errorFlag = false;

        var emailError = document.getElementById("emailError");

        emailError.style.display = "block";
    }

    return errorFlag;
}

function clearErrors(){

    var nameError = document.getElementById("nameError");

    nameError.style.display = "none";

    var phoneError = document.getElementById("phoneError");

    phoneError.style.display = "none";

    var emailError = document.getElementById("emailError");

    emailError.style.display = "none";

}
9
  • clear error method is simply making the style to none and not making values reset. Commented Apr 22, 2016 at 3:44
  • I'm not quite sure what you mean. The only purpose of clearErrors is to hide the error messages on the second run. Shouldn't the values of the vars be set again every time the script runs? Commented Apr 22, 2016 at 3:46
  • Can I suggest to put the regex to the 'pattern' attribute of the element? Commented Apr 22, 2016 at 3:48
  • Your email validator is a string, not a regex. You need /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i, or new RegExp("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$", "i") if want to use a string. Commented Apr 22, 2016 at 3:50
  • I was taught to put the regex into a string before using .match(). How should i use it instead? Commented Apr 22, 2016 at 3:51

1 Answer 1

2

Your validator will fail on the email, because you are feeding a string to .match, when it needs a regex.

You also have to call .match on the email itself, with the regex as the argument.

You also need to negate the return value to check if it does not match, or use .test.

This bit:

var reStr = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$";
if((reStr.match(emailVal))){

Should be replaced with:

var re = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i;
if(!emailVal.match(re)){

Of if you can't use a regex literal for some reason:

var re = new RegExp("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$", "i");
if(!emailVal.match(re)){

Or using .test instead of .match:

var re = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i;
if(!re.test(emailVal)){

Note the i for case-insensitive matching, so emails don't have to be entered in all-caps.

Sign up to request clarification or add additional context in comments.

10 Comments

So using both fixes, the form automatically submits no matter if there are errors on the first two inputs. I'm assuming there's something else here that I've messed up.
@Spork Sorry, I missed that you were calling match on the regex, not the string, see the updated answer.
I think the problem here is the behavior being different on the second run, not the validation.
@Spork I just realized, you also need to negate the match for the if to be the failing case. Edit: Oh, looks like you just caught that too. :)
Yeah, that was a dumb miss by me
|

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.