I'm trying to validate a HTML form using JavaScript, and I've tested all the validation functions seperately and when I do that they work fine, but when returning them all together for some reason only my validatename
and validateEmail
functions work, and I was wondering if anyone could give me tips on what I need to do to make it work? Here is the code I have:
<script type="text/javascript">
function validatename()
{
var x = document.forms["feedback"]["fullname"].value;
if (x == "") {
alert('Name is not filled out');
return false;
}
} // validatename()
</script>
<script type="text/javascript">
function validateEmail()
{
var validemail = document.forms["feedback"]["email"].value;
if (/(.+)@(.+)\.(.+)/.test(validemail)){
return true;
}
else {
alert('Invalid email or email not filled out')
return false;
}
} // validateEmail
</script>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script>
function agecheck() {
if($('input[name=age]:checked').length<=0)
{
alert("Please select your age");
return false;
}
} // agecheck
</script>
And then I return them in my HTML body through this:
<form name = feedback onsubmit=" validateEmail() && validatename() && agecheck() ">
EDIT: When I run the functions by exchanging the && for ; so I don't get a short circuit evaluation all of the functions do run, but for some reason if both validatename and validateEmail return as not false then agecheck won't run and thus won't throw up its alert when it should.
EDIT2: Ok so for some reason it was because of the fact that validatename either returned as false or undefined that the validateage wouldn't return, and I just fixed it to return as true or false rather than undefined or false which somehow fixed it. Thanks all for your help