onsubmit multiple javascript functions

守給你的承諾、 提交于 2019-11-27 04:34:01

Use & instead of &&.

&& uses short-circuit evaluation and so doesn't evaluate the right-hand operand if the left-hand operand is falsy.

& always evaluates both operands.

onsubmit="return !!(validateName() & validatePhone()
                      & validateAddress() & validateEmail());"

EDIT: Sorry, I forgot that & won't return an actual boolean. Wrap the expression in parentheses and use a double ! to convert it as (now) shown. (You wouldn't need to worry if using the result in an if test, but the return value from the event handler should be an actual boolean.)

P.S.: Here's a rather clunky demo to show that all four functions get called but produce the overall true or false result that you need: http://jsfiddle.net/nnnnnn/svM4h/1/

Change your code to use AND instead of OR

onsubmit="return validateName() && validatePhone() && validateAddress() && validateEmail()"

The reason being that if you use OR once that first function returns true javascript won't bother to check the other functions. Also you don't want to use OR because if the first check was false and second is true the form will still submit since at least one of your checks is TRUE.

See fiddle to show this works, both checks execute and show an alert message but because check2 returns false the form doesn't submit - if you want play around with the return results of checks1 and check2 functions and the use of && and ||

After understanding the problem better, you need to use a single &, but because true & true returns 1 and not true you need to write it like this

onsubmit="return ((validateName() & validatePhone() & validateAddress() & validateEmail()) == 1)"

if all you need is make sure all functions return true before proceeding, then use && not || and to insure that the expression does not get evaluated pre-maturally by the return function put some extra brackets

<form id="myForm" name="myForm" action="checked.html" method="post"
onsubmit="return (validateName() && validatePhone() && validateAddress() &&
validateEmail() )">

Create a wrapper function, which call each function and checks their return value. At any time if the return value is false you can either stop the form submit by returning false at that time or evaluate the rest of the things and finally return false.

So JS a code that will do what you want is :

function validateForm() {
    var isFormValid = true;
    isFormValid &= validateName();
    isFormValid &= validatePhone();
    isFormValid &= validateAddress();
    isFormValid &= validateEmail();
    return isFormValid? true:false;
}

Now in the form simply use :

<form id="myForm" name="myForm" action="checked.html" method="post" onsubmit="return validateForm();">

Here is a working JSFiddle Example.

EDIT : Made a boo boo below, corrected.

Using OR was incorrect, but the reason AND is not working how you are expecting is because of Short-circuit_evaluation.

  • OR : Evaluation stops on first operand which is true and true is returned
  • AND : Evaluation stops on first operand which is false and false is returned
  • onsubmit="return validateName() && validatePhone() && validateAddress() && validateEmail()"

    use this code

    To validate my form I simply created a function called validateForm(){} then inside this function I put all the code which is needed for full validation. Then used onsubmit="validateForm()". It checks through it in order and only submits if all conditions are true.

    Following method check first validateName() function and it will process to check next function if only first function is true, also to fire third function need to return true of first & second function. So just imagine if all function are invalid & even you have different error message, you will see only error message of first function. Once validateName() function pass, you may see the error message of validatePhone() ect.

    <form id="myForm" name="myForm" action="checked.html" method="post"
    onsubmit="return (validateName() && validatePhone() && validateAddress() &&
    validateEmail() )">
    

    You can't use above if you going to show error messages according to above all validations on first submit & you can use following method if you want to fire all function (or display all validation errors) on submit.

    <form id="myForm" name="myForm" action="checked.html" method="post"
    onsubmit=" if( ( validateName() && validatePhone() && validateAddress() &&
    validateEmail() ) == false) { return false; } ">
    
    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!