Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return multiple functions

Tags:

javascript

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();

}
like image 541
user1510389 Avatar asked Jul 08 '12 18:07

user1510389


People also ask

Can a function return 2 functions?

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.

How do you return multiple functions in Python?

To return multiple values from a Python function, use a comma to separate the return values.

Can I make a function return multiple 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.

How do you return two functions in JavaScript?

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.


1 Answers

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;
}
like image 200
Dmitry Osinovskiy Avatar answered Sep 28 '22 04:09

Dmitry Osinovskiy