php - Validating Form with Javascript before running Ajax function -


let me try explain myself clearly. want use javascript validate form , once filled in, execute ajax have ready.

this javascript code want use validate form:

function validateform(frm) {   var age = frm.age.value,   height_ft = frm.height_ft.value,   height_in = frm.height_in.value,   weight = frm.weight.value;    if (isnan(age) || age == ""){     alert("please enter age, numbers only.");     frm.age.focus();     return false;   }    if (isnan(height_ft) || isnan(height_in) || height_ft == "" || height_in == "") {     alert("please enter height, numbers only.");     return false;   }    if (isnan(weight) || weight == "") {     alert("please enter weight, numbers only.");     frm.weight.focus();     return false;   } } 

now below ajax code:

$("#submit").click( function() {   $.post( $("#weightfrm").attr("action"),          $("#weightfrm :input").serializearray(),          function(info) {              $("#ack").empty();             $("#ack").html(info);          }); });  $("#weightfrm").submit( function() {    return false;     }); 

now ajax code works flawlessly, validate information first , submit once filled in. taken.

 <form id="weightfrm" action="form_submit.php" onclick="return validateform(this);" method="post"> 

do

function validateform(frm) {     var age = frm.age.value,         height_ft = frm.height_ft.value,         height_in = frm.height_in.value,         weight = frm.weight.value;      if (isnan(age) || age == ""){         alert("please enter age, numbers only.");         frm.age.focus();         return false;     }     else if (isnan(height_ft) || isnan(height_in) || height_ft == "" || height_in == "") {         alert("please enter height, numbers only.");         return false;     }     else if (isnan(weight) || weight == "") {         alert("please enter weight, numbers only.");         frm.weight.focus();         return false;     }else {     $.post( $("#weightfrm").attr("action"),              $("#weightfrm :input").serializearray(),              function(info) {                  $("#ack").empty();                 $("#ack").html(info);              });      $("#weightfrm").submit( function() {        return false;         });      }} 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -