onsubmit multiple javascript functions

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/

Leave a Comment