I am currently using jQuery bassistance validation or the jqueryvalidation plugin (they renamed it) and I have it validated like this:
if($.fn.validate) { $("#frmNewOrder").validate({ rules: { txtAmount: { required: true, digits: true }, ddlAccount: { required: true }, ddlBank: { required: true } } }); }
Now, depending on the amount, I will need to require or not an additional field of the drop-down list, and depending on this drop-down list, enter another text. so that:
if($('#txtAmount').val() >= 10000){ //add ddlReason required } if($('#ddlReason').val() == 'o'){ //add txtReason required }
I tried to add the css class 'required', like other fields, but I accept it if they are not inside the rules, then this does not work? I tried adding rules like this:
$( "#ddlReason" ).rules( "add", { required: true});
and this also does not work. any suggestions?
EDIT: link to jquery validation that I am using. if I used the rules outside of it, it throws this error:
Uncaught TypeError: Cannot read property 'settings' of undefined
on line 124:
var settings = $.data(element.form, 'validator').settings;
and using the sentences that I saw elsewhere, I did this, which causes an error:
var defaultrules = { txtAmount: { required: true, digits: true } } if($.fn.validate) { $("#frmNewOrder").validate(); addRules(defaultrules); }
EDIT 2: here's the html markup, hope this 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>