validate one field depending on another field value
Hi All,
I am new in jquery and learning it.I am going to make a form validation where if the radio button is checked then the form validation will take place in particular one field ie file field and all other fields will be validated normally.
Here is my code:
$(document).ready(function () {
//validate form
$("form[name='rgt_form']").validate({
debug: false,
rules: {
txt_url: "required",
txt_title: "required",
txt_qty: {
required: true,
digits: true
},
txt_price: { // compound rule
required: true,
digits: true
},
picture_upload: { // this field has to be validated if the radio button is clicked.
required: true,
accept: "jpg|gif|png|jpeg"
},
}
});
<form action="index.php" method="post"
enctype="multipart/form-data" name="rgt_form" class="forms">
<input id="rad_no_image" name="rad_image_option"
checked="checked" value="no_image" type="radio">None
<input id="rad_upload_image" name="rad_image_option"
value="upload" type="radio">Upload Picture
<div id="picture_upload_div" style="display: none;">
<input class="rgt_inputtext" name="picture_upload" id="picture_upload"
value="Upload Image" type="file">
<span class="rgt_field_hint" style="padding-left: 140px;">(4MB
max. Allowed jpg,gif,png)</span>
</div>
</form>
Please suggest how can I get to know which radio button is clicked and if the value is "upload" then picture_upload will be validated else will be ignored.
Thanks,
Raj