Blessings of Jquery “each” and Jquery selectors – asp.net mvc form validation





Let me explain the following code and what i think about the topic
“Blessings of Jquery “each” and Jquery selectors – asp.net mvc form validation”

<script type=”text/javascript”>
$(document).ready(function() {
var isValid = false;
$(‘input:text’).each(function() {
if ($(this).val() == “0”) {
$(this).val(“”);
}
});
$(“#btSubmit”).click(function() {
$(‘input:text’).each(function() {
if ($(this).val().length > 0) {
isValid = true;
return;
}
});
if (isValid) {
$(“form”).submit();
}
else {
$(“#errorMessage”).html(“Please enter search criteria.”);
return false;
}
});
});
</script>

This javascript is iterating through all text boxes on the form and checks if the text box values is zero then it makes those text boxes empty, then on submit button click event it checks if all text boxes are empty then it will not submit the form.

In following line of code JQuery selector “input:text” is checking text boxes on the form and “each” is iterating through all controls on the form the combination of selector and each is iterating through all text boxes on the form.

$(‘input:text’).each( [your code goes here])




Tags: , , , ,

Comments are closed.