开发者

how to avoid form from submitting if both these values are valid

Hi Below is the code that i am using for my form to validate id's entered in two input fields.All code is working fine.Only thing is that how to stop form from submitting if both the fields are not valid as i am disabling and enabling submit button if record return is true.but if any one field is valid then submit button is enabling and allowing user to submit form.

<form>
<input type="text" id="1" value=""/>
<input type="text" id="2" value=""/>
<input type="submit" value="submit"  id="submitbutton" >
</form>
<script type="text/javascript">
            $$(document).ready(function() {             
            $$('#1').blur(function() {              
            var id = $$('#1').val();             
            $$.getJSON("user.cfc?method=getRequests&returnformat=json&queryFormat=column",{"status":id}, function(res1,code) {

                if(res1.ROWCOUNT > 0){  

                  for(var i=0; i<res1.ROWCOUNT; i++) {                      
                    s = "<span class='dPostTxt'>" + res1.DATA.ID[i] + " " + res1.DATA.FIRST_NAME[i] + " " + res1.DATA.LAST_NAME[i] + " " + "is valid." + "</span>";
                  };                  
                 $$('#submitbutton').removeAttr('disabled');//now id is valid so enabling submit button                  
                } 
                else {
                  $$('#submitbutton').attr('disabled', 'disabled'); //error with id so disabling button 
                  alert(" ID Is Not Valid.Please Enter Valid ID");
                  var s = "Entered  ID Is Not Valid.";                                  
                }     

                $$("#results").html(s);                
                },"json");                  
           });  }); 
    </script>

<script type="text/javascript">
            $$(document).ready(function() {              
            $$('#2').blur(function() {              
            var id = $$('#2').val();             开发者_C百科
            $$.getJSON("user.cfc?method=getRequests&returnformat=json&queryFormat=column",{"status":id}, function(res,code) {

                if(res.ROWCOUNT > 0){  

                  for(var i=0; i<res.ROWCOUNT; i++) {                   
                    s = "<span class='dPostTxt'>" + res.DATA.ID[i] + " " + res.DATA.FIRST_NAME[i] + " " + res.DATA.LAST_NAME[i] + " " + "is valid." + "</span>";
                  };
                 $$('#submitbutton').removeAttr('disabled');//now id is valid so enabling submit button                  
                } 
                else {                  
                  $$('#submitbutton').attr('disabled', 'disabled'); //error with id so disabling button 
                  alert(" ID Is Not Valid.Please Enter Valid ID");
                  var s = "Entered  ID Is Not Valid.";                                  
                }     

                $$("#results1").html(s);                
                },"json");                  
           });  }); 
    </script>


$("form").submit(function() { 
    bool valid = goValidateForm();
    if(!valid)
        return false; //prevents form from submitting
    else
        goDoSomeCoolStuff();

});


assign your checks to fire on $("form").submit() instead of onBlur of the fields. that way you can check both states at the same time, maybe fire a alert to the page if your form values are wrong &etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜