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>
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.