0

I'm trying to execute two functions at once validating online form. First form works fine but the second is not being executed. Can anyone point out what is wrong with my code?

function both(){
    validation();
    regex();
}

function validation(){
    var valid = true;
    var error = [];

    if (document.check_in.forename.value == ""){
        error.push("First Name");
        valid = false;
    }
    if (document.check_in.surname.value == ""){
        error.push("Surname");
        valid = false;
}
    if (valid == false) {
        alert("Please fill in the following fields:\n" + error.join("\n") + ".");
    }
    return valid;
}

function regex(){
    var fnameRE = /^[a-zA-Z]+$/;
var valid_reg = true; 
    var error_reg = [];

    if (!fnameRE.test(document.check_in.forname.value)) {
        error_reg.push("Please use characters only in First Name field.");
        valid_reg = false;
    }   
    else if (valid_reg == false) {
        alert("Please fill s:\n" + error_reg.join("\n") + ".");
    }
    return valid_reg;
}

And HTML:

<form name="check_in" method="POST" action="form.html" onSubmit="return both()"> 

2 Answers 2

3

I think you need something like

function both(){
    return (validation() && regex())
}

This way "both" will return true only if both function passed validation. Otherwise it returns false

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

Comments

0

I am not sure but this might be it....you are calling two functions with return statement in both, as soon as it encounters the first return statement, your code execution ends. Did you try putting an alert in your regex() function? Can you try that and prove my statement right/wrong here.

your workaround:

function both(){
    var valid = validation();
    var regexvalid = regex();

    if(valid && regexvalid)
          return true;
    else
          return false;
}

Comments

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.