Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return true or false not working in JavaScript

My return values are not working, and I need them to work so I can validate the page. I have a function in a function because there will be more code written which will require that kind of setup.

Here is the JavaScript code:

var postalconfig = /^\D{1}\d{1}\D{1}\-?\d{1}\D{1}\d{1}$/;

function outer(){
    function checkpostal(postal_code){
      if (postalconfig.test(document.myform.postal_code.value)) {
        alert("VALID SSN");
        return true;
      } else {
        alert("INVALID SSN");
        return false;
      }
    }
  checkpostal();
}

And the HTML:

<form name="myform" action="index.php" onSubmit="return outer();" method="post">
    Postal Code <input name="postal_code"  type="text" />
    <input name="Submit" type="submit"  value="Submit Form" >
</form>
like image 569
Christopher Hunt Avatar asked Jul 21 '26 22:07

Christopher Hunt


1 Answers

Change checkpostal(); to return checkpostal();

like this:

var postalconfig = /^\D{1}\d{1}\D{1}\-?\d{1}\D{1}\d{1}$/;

function outer(){   

  function checkpostal(postal_code) {
    if (postalconfig.test(document.myform.postal_code.value)) {
      alert("VALID SSN");
      return true;
    } else {
      alert("INVALID SSN");
      return false;
    }
  }

  return checkpostal();

}
like image 195
Martin Jespersen Avatar answered Jul 23 '26 11:07

Martin Jespersen