简体   繁体   中英

javascript skips validation form

I have html running javascript after form,

form name="mortgage" method="post" action=" "onsubmit="return completeFormValidation();">

And javascript code for validation,

function completeFormValidation() {

location();


} // End of completeFormValidation

function location(){



var numradio = document.mortgage.propLocation.length;
var selected="";

for(var i=0; i < numradio; i++){

    if(document.mortgage.propLocation[i].checked == true){
        selected += "Selected radio item " + i;

    }
}

if(selected == ""){

    document.getElementById("reserved").innerHTML = "<p> none radio selected </P>";
    return false;

}

}

The code works perfectly fine in dream weaver but in browsers doesn't seem to work instead it will submit even if radio buttons aren't selected. Please help me out thanks.

because you are not returning the "FALSE/TRUE" value from completeFormValidation function.. And Change the name of the function Location is reserved by JavaScript.

check the jsfiddle

function completeFormValidation() {

return my_location();

}

its always better to return the value from location true OR false

you can modify your my_location() as below

function my_location(){

var numradio = document.mortgage.propLocation.length;
var selected="";

for(var i=0; i < numradio; i++){

    if(document.mortgage.propLocation[i].checked == true){
        selected += "Selected radio item " + i;

    }
}

if(selected == ""){

    document.getElementById("reserved").innerHTML = "<p> none radio selected </P>";
    return false;

}
else{
    return true;
}

}

try to replace

function completeFormValidation() {

location();

} // End of completeFormValidation

by

function completeFormValidation() {

return location();

} // End of completeFormValidation

Rename your location function. location is a native property of the window object and can not be redefined.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM