最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Return true or false not working in JavaScript- Stack Overflow

programmeradmin6浏览0评论

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>

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>
Share Improve this question edited May 12, 2011 at 17:25 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Feb 3, 2011 at 22:02 Christopher HuntChristopher Hunt 1071 gold badge3 silver badges11 bronze badges 5
  • What do you mean by "not working"? Do you mean that the function returns the correct result, but the submission happens anyway? – Jacob Mattison Commented Feb 3, 2011 at 22:05
  • with better formatting sometimes these things popout at us. I suggest a notepad type program like notepad++ if you're not already using a javascript oriented IDE to help you keep your code organized and properly indented. (reindent as C style code usually works pretty well). – jcolebrand Commented Feb 3, 2011 at 22:09
  • Seconding @drachenstern's point. jsbeautifier is a good tool for this too. – lonesomeday Commented Feb 3, 2011 at 22:12
  • Thanks guys I'll work on better formatting for the future. – Christopher Hunt Commented Feb 3, 2011 at 22:19
  • Well the fact that you're grasping the concepts of closures is good and well enough. Keep up the good work and e on back with more questions as you get them ;) – jcolebrand Commented Feb 3, 2011 at 22:27
Add a ment  | 

4 Answers 4

Reset to default 7

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

}

The problem here is that you are getting the return value of outer, but outer doesn't return anything. return true (or false) only affects the current function, in this case, checkpostal.

You need to get outer to return the return value of checkpostal:

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

Looks like at the end of outer() it should be

return checkpostal();

rather than just

checkpostal();

The call to checkpostal() may return correctly, but onsubmit won't get the result, since outer() isn't returning anything.

You'll want to return the call to checkpostal:

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

}
发布评论

评论列表(0)

  1. 暂无评论