How can ignore some text box ignore for validation checking. Below code is applied for all text but I need to leave few text box blank, but due to the code I am not able to submit the form with optional text box. Kindly refer the jquery code which is used for validating ALL text box
<script>
$(document).ready(function() {
$('#btnSubmit').click(function(e) {
var isValid = true;
$('input[type="text"]').each(function() {
if ($.trim($(this).val()) == '') {
isValid = false;
$(this).css({
"border": "1px solid red",
"background": "#FFCECE"
});
}
else {
$(this).css({
"border": "",
"background": ""
});
}
});
if (isValid == false)
e.preventDefault();
else
alert('Thank you for submitting');
});
});
</script>
add a class on text boxes which are mandatory:
<input type="text" value="1" class="mandatory"/>
<input type="text" value="2"/>
and then:
$('input[type="text"].mandatory').each(function() {
});
Your input selector is too general. One way to go from here is to add a class to all the inputs you want to validate. E.g.:
<input type="text" class="validate">
<input type="text">
<input type="text" class="validate">
Then, all you need to do, is change the selector in your validation function
from this:
$('input[type="text"]')
to this:
$('input.validate')
That way the only inputs to be validated are the ones with class="validate"
. The middle one won't be validated.