Hi,
I am a newbie to jQuery and still learning the ropes. I would greatly appreciate your help in resolving the following issue:
I am using jquery.validate.js plugin to validate a form.
The form has two groups of radio buttons with names ( g1 and g2 ). It also has other elements like textboxes and select boxes.
Buttons are defined as :
Yes<input id="g1" type="radio" name="g1" value="yes" checked="checked" onClick="$('#div2').slideToggle(400);">
No<input id="g1" type="radio" name="g1" value="no" onClick="$('#div2').slideUp();">
Yes<input id="g2" type="radio" name="g2" value="yes" checked="checked" onClick="$('#div4').slideToggle(400);">
No<input id="g2" type="radio" name="g2" value="no" onClick="$('#div4').slideUp();">
div2 has elements like:
<TD><input type="text" size="30" name="prodid[]" onblur="validateInput(this.value)"/></TD>
<TD><input type="text" size="50" name="prodname[]" /></TD>
div4 elements like:
<TD><input type="text" size="30" name="batchid[]" onblur="validateInput(this.value)"/></TD>
<TD><input type="text" size="50" name="qty[]" /></TD>
Rule : When g1(Yes) is checked and g2(No) is checked, only div2 elements should be validated and not div4 elements and vice-versa. If both are Yes, then both divs should be vaildated
I have tried different combos as below, but to no avail. The problem is that ALL FORM elements are being validated instead of the selective div elements.
- $(document).ready(function() {
$("#Form").validate({
rules: {
"prodid[]": {
required: "#g1:checked"
/*required: function(element){
if($("input[name=g1]:checked").val()=='yes')
$("#prodid[]").valid();
}*/
},
"batchid[]": {
required: "#g2:checked"
//required: "#g2:checked.val()=='yes'"
/*required: function(element){
if($("input[name=g2]:checked").val()=='yes')
$("#batchid[]").valid();
}*/
}
}, debug:true
});
$("#g1").click(function(){
if($("#g1:checked").val()=='yes')
$("#prodid[]").valid();
});
$("#g2").click(function(){
if($("g2:checked").val()=='yes')
$("#batchid[]").valid();
});
});
validateInput is another javascript function to match regex that is used for prodid and batchid:
-
function validateInput(strValue){
var objRegExp = /^(?!.*(--| | -|- ))(?!^.*[-]$)[A-Za-z0-9][A-Za-z\d\s-]{3,19}$/;
if (!objRegExp.test(strValue)){
alert("ID cannot start or end with a dash or contain consecutive dashes or spaces");
}
}
Appreciate your help and thanks in advance for any help/guidance.
Cheers.