I am new to javascript, so I apologize if this is all totally wrong, but I'm trying to validate a form right now, and I have the simple functions for testing. I want the function validateForm() to return all three of the functions checkName(), checkEmail, and checkMessage(). The way I have the validateForm() function, it only runs the checkName() function. Any ideas?
function checkName(){
var name=document.forms["contactForm"]["Name"].value;
if(name==null || name==""){
$("input#name").css("border-color", "#ff0000");
$("input#name").attr("placeholder","Your name is required");
return false;
}
else {
$("input#name").css("border-color", "#00a8ff");
$("input#name").attr("placeholder","");
return true;
}
}
function checkEmail(){
var email=document.forms["contactForm"]["Email"].value;
if(email==null || email==""){
$("input#email").css("border-color", "#ff0000");
$("input#email").attr("placeholder","Your email is required");
return false;
}
else {
$("input#email").css("border-color", "#00a8ff");
$("input#email").attr("placeholder","");
return true;
}
}
function checkMessage(){
var message=document.forms["contactForm"]["Message"].value;
if(message==null || message==""){
$("textarea#message").css("border-color", "#ff0000");
$("textarea#message").attr("placeholder","Your message is required");
return false;
}
else {
$("textarea#message").css("border-color", "#00a8ff");
$("textarea#message").attr("placeholder","");
return true;
}
}
function validateForm(){
return checkName() && checkEmail() && checkMessage();
}
A function may return multiple values by returning a sequence. Multiple assignment (see =) or multiple local assignment (see :=) can be used to assign the values to separate variables, if the number of values to be returned is known. Simple assignment may be used if the number of values to be returned is unknown.
To return multiple values from a Python function, use a comma to separate the return values.
You can return multiple values from a function using either a dictionary, a tuple, or a list. These data types all let you store multiple values. There is no specific syntax for returning multiple values, but these methods act as a good substitute.
JavaScript doesn't support functions that return multiple values. However, you can wrap multiple values into an array or an object and return the array or the object. Use destructuring assignment syntax to unpack values from the array, or properties from objects.
Operator && executes left operand (checkName in your case), and, if it is false, immediately returns false without executing right operand. So, you need to manually execute each of your function and only then connect them via &&.
function validateForm(){
var a = checkName();
var b = checkEmail();
var c = checkMessage();
return a && b && c;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With