validation - Javascript: Need help validating two aspects of a form, which will then enable the SUBMIT button -


my name romano , i'm looking guidance on form validation process.
have registration form has validation process against username field , terms , conditions checkbox.

the validation have against checkbox easy:

<script type="text/javascript"> function apply()  { document.frm.sub.disabled=true; if(document.frm.chk.checked==true) { document.frm.sub.disabled=false; } if(document.frm.chk.checked==false) { document.frm.sub.enabled=false; } } </script> 

and form field calls function:

this works fine.

i have block of javascript code validates username meets minimum requirements , ensures not exist in database. block of code larger, , looks this:

 <script type="text/javascript">  $(document).ready(function()  { $("#username").change(function()  {  var username = $("#username").val(); if(username.length > 5)//if lenght greater 5 characters { $("#availability_status").html('<img src="loader.gif" align="absmiddle">&nbsp;checking availability...');   $.ajax({  //make ajax request  type: "post",    url: "ajax_check_username.php",    data: "username="+ username,   success: function(server_response){     $("#availability_status").ajaxcomplete(function(event, request){   if(server_response == '0') {  $("#availability_status").html('<img src="images/available.png" align="absmiddle">   <font color="green">available </font>  ') }   else  if(server_response == '1') {    $("#availability_status").html('<img src="images/not_available.png" align="absmiddle">     <font color="red">not available </font>')   }        });   }     });     }    else    {     $("#availability_status").html('<font color="#cc0000">username short</font>');   }   return false;   });     });   </script> 

and code works too.

what i'd have done integrate both blocks of code submit button enabled when conditions true. in other words, when checkbox checked , username greather 5 characters , not exist in database.

so, easy task? sounds should i'm @ loss (probably looking @ code past 2 days).

thanks!!!

you want validation triggered change of either checkbox or name. given you're doing ajax check see if username valid i'd set flag indicate whether current name valid ajax runs when name changes , not checkbox. perhaps this:

$(document).ready(function() {     var usernameok = false,   // assume username starts out blank         termschecked = false;      function setsubmitenabling() {         document.frm.sub.disabled = !(usernameok && termschecked);     }       $("#yourcbidhere").click(function() {         termschecked = this.checked;         setsubmitenabling();     });      $("#username").change(function() {         var username = this.value;         if(username.length <= 5) { //if name short             $("#availability_status").html('<font color="#cc0000">username short</font>');             usernameok = false;             setsubmitenabling();             return;         }         $("#availability_status").html('<img src="loader.gif" align="absmiddle">&nbsp;checking availability...');         usernameok = false;         setsubmitenabling();         $.ajax({             type: "post",               url: "ajax_check_username.php",               data: "username="+ username,              success: function(server_response){                 if(server_response == '0'){                      $("#availability_status").html('<img src="images/available.png" align="absmiddle"><font color="green">available </font>');                     usernameok = true;                 } else if(server_response == '1') {                       $("#availability_status").html('<img src="images/not_available.png" align="absmiddle"><font color="red">not available </font>');                     usernameok = false;                 }                 setsubmitenabling();             }         });     }); }); 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -