Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a textbox and done the validation in that without entering any fields I won't be able to enter the second textbox. But now the requirement is if I'm pressing backspace and entering the tab button means field level validation is not working i.e it moves to the second textbox.


The code I used
JavaScript
<script type="text/javascript" src="../assets/global/plugins/jquery-validation/js/jquery.validate.min.js"></script>
<script type="text/javascript" src="../assets/global/plugins/jquery-validation/js/additional-methods.min.js"></script>
<script src="../assets/admin/pages/scripts/form-wizard.js"></script>
<script type="text/javascript">
$().ready(function () {
    $('#PageContent_txt_Propertytypename').focus(function () {
        name_propertytype = document.getElementById('PageContent_txt_Propertytypename').value;
        if (name_propertytype== '') {
            $('.fld1-error').html('This field is required');
            $('#PageContent_txt_Propertytypename').css('border-color', 'red');
            document.getElementById('PageContent_txt_Propertytypename').focus();
            return false;
        } else {
            $('.fld1-error').html('');
            $('#PageContent_txt_Propertytypename').css('border-color', 'green');
        }
});
});
 
</script>


Thanks and Regards
M.G.Balamurugan
Posted
Updated 12-Jan-15 2:31am
v4
Comments
CHill60 12-Jan-15 8:13am    
What code have you used for the validation?

1 solution

Validating fields should be done when the form (I assume the textfield is in one) is about to be submitted. ie

JavaScript
$(document).on("submit", "#formID", function(e) {
  if (!validationPassed  ) {
    e.preventDefault();
  }
})


This is more efficient than validating each field.

To save you time though, you could use jquery-validation which you have installed. you could just use $("#formID").validate() see JQuery-validation documentation[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900