onsubmit multiple javascript functions

后端 未结 7 604
你的背包
你的背包 2020-11-30 08:31

Does anyone know how to onsubmit multiple functions? What I intented to do was: onsubmit checks if all these functions return true, if all of them return true, then I do the

7条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 08:59

    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)"

提交回复
热议问题