I am currently using the bassistance jquery validation or the jqueryvalidation plugin(they renamed it) and I have it validating a form like so:
if($.fn.validate) {
$("#frmNewOrder").validate({
rules: {
txtAmount: {
required: true,
digits: true
},
ddlAccount: {
required: true
},
ddlBank: {
required: true
}
}
});
}
Now depending on the amount I need to require or not an extra field a dropdownlist and depending on that dropdownlist another text input. so it's like:
if($('#txtAmount').val() >= 10000){
//add ddlReason required
}
if($('#ddlReason').val() == 'o'){
//add txtReason required
}
I've tried adding the css class 'required' like the other fields but I take it if they are not inside the rules then it doesn't work? I've tried adding rules like this:
$( "#ddlReason" ).rules( "add", {
required: true});
and that doesn't work either. any suggestions?
EDIT: Here's the link to the jquery validate that I'm using. if I used rules outside it gives this error:
Uncaught TypeError: Cannot read property 'settings' of undefined
on line 124:
var settings = $.data(element.form, 'validator').settings;
and using the suggestions I've seen in other places i've done this which is what causes the error:
var defaultrules = {
txtAmount: {
required: true,
digits: true
}
}
if($.fn.validate) {
$("#frmNewOrder").validate();
addRules(defaultrules);
}
EDIT 2: here's the html markup, hope it helps.
<form id="frmNewOrder" name="frmNewOrder" action="" method="post">
<label>Amount</label>
<input type="text" name="txtAmount" id="txtAmount" class="required" />
<button id="calculate" type="button">Calculate</button>
<div id="dvreason" style="display:none;">
<select id="ddlReason" name="ddlReason">
<option></option>
<option value="i">Option 1</option>
<option value="o">Other</option>
</select>
<div id="dvother" style="display:none">
<label>Other reason</label>
<input type="text" name="txtOther" id="txtOther" />
</div>
</div>
<input type="submit" name="send" id="send" value="Send" />
</form>
class="required"or.rules('add')instead of withinrulesdeclaration in.validate(). There must be something else wrong with your code. Show enough code for constructing a working demo. All relevant jQuery and HTML.