0

I made a function which validates a form and works fine but I now want to break it down into 3 separate functions.

I now have a function which is called by the form being submitted which declares some arrays and runs the three functions. When it was all one big function the various if statements that found errors would return false; which would then go back to the form and stop it sending.

However now that I've got functions within a function I can't figure out how to get that message 'false' back to the form.

Below is the function called by the form submit button followed by the main function it calls. I tried creating an empty variable which is returned instead of false which is then is assigned the value false by the validateSignup function but it didn't work.

function validateSignup()
{
 // Declaring Arrays (deleted array contents)
 var errorSpansArray=[whatever];   
 var regexErrorArray=[whatever];
 var regexArray=[whatever];

validateText(0,6,errorSpansArray, regexErrorArray, regexArray);
passMatch();
genderCountryCheck()  
}

function validateText(formNumber, numberElements, errorSpansArrayName, regexErrorArrayName, regexArrayName)
 {              
                     for (x = 0; x<numberElements; x++)
                   {
                   var spanName = errorSpansArrayName[x];
                   var textError = document.getElementById(spanName);
                   var y=document.forms[formNumber].elements[x];
                   if (!y.value)
                   {           
                    errorMessage(0,spanName,x);
                   return false;
                   }
                   if(!regexArrayName[x].test(y.value)){  
                   textError.innerHTML = regexErrorArrayName[x];
                   return false;

                   }
               }

UPDATE: Thanks for your responses. I have found a solution that seems to work for me.

 function validateSignup()
 {
 // Declaring Arrays (deleted array contents)
 var errorSpansArray=[whatever];   
 var regexErrorArray=[whatever];
 var regexArray=[whatever];

var returnValidateText=validateText(0,6,errorSpansArray, regexErrorArray, regexArray);
var returnPassMatch = passMatch();
var returnGenderCountry = genderCountryCheck();

if (returnValidateText || returnPassMatch || returnGenderCountry === false)
    {
    return false;            
    }
    else{
    return true;    
    }

}

3 Answers 3

1

If you call the function it returns a value

   var formIsValid = function validateText(....)

should do the trick.

function validateSignup()
{
     // Declaring Arrays (deleted array contents)
     var errorSpansArray=[whatever];   
     var regexErrorArray=[whatever];
     var regexArray=[whatever];

     var formIsValid = false;

    formIsValid = validateText(0,6,errorSpansArray, regexErrorArray, regexArray);
    formIsValid = passMatch();
    formIsValid = genderCountryCheck()  
}
Sign up to request clarification or add additional context in comments.

Comments

0

One way is to just check the individual function returns directly and return based on that

if (!validateText(0,6,errorSpansArray, regexErrorArray, regexArray)) {
  return false;
}
if (!passMatch()) {
  return false;
}    
if (!genderCountryCheck()) { 
  return false;
}

Although it's shorter to use a single conditional

return
  validateText(0,6,errorSpansArray, regexErrorArray, regexArray) &&
  passMatch() &&
  genderCountryCheck();

Comments

0

In javascript return false means false will be returned as value where the method is called. So you need something like

If(validateText()){
    return true;
}

And similarly rest of the code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.