返回true或false不能在JavaScript中工作

Chr*_*unt 2 html javascript forms return-value

我的返回值不起作用,我需要它们才能工作,所以我可以验证页面.我在函数中有一个函数,因为会编写更多的代码,需要这种设置.

这是JavaScript代码:

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();
}
Run Code Online (Sandbox Code Playgroud)

和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>
Run Code Online (Sandbox Code Playgroud)

Mar*_*sen 7

更改checkpostal();return checkpostal();

像这样:

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

}
Run Code Online (Sandbox Code Playgroud)