Click here to Skip to main content
15,881,804 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I am using asp.net mvc Client-Side Validation .
Here I am generating the html of input textboxes dynamically and also adding the validation messages and validation rules dynamically for these inout text boxes.

JavaScript
Html += '<td id="tdMaxCharge" ><input type="number" value="' + ProrataMaximumCharge + '" id="' + prorateinstanceId + '_MaximumCharge" class="pro-rata-val" name="' + id + '.MaximumCharge" />';
            Html += "<div><input class='ajax-auto smallTxtEntry1 ui-autocomplete-input valid pro-rata-val temp--uom'  placeholder='UOM' data-val='true' data-val-required='Please enter Maximum Charge UOM' display-name='Maximum Charge UOM' id='" + prorateinstanceId + "_MaximumChargeCurrencyName' name='" + id + ".MaximumChargeCurrencyName' type='text' value='" + ProrataCurrencyName + "' autocomplete='off' role='textbox' aria-autocomplete='list' aria-haspopup='true'/>";
            Html += "<input data-val='true' data-val-required='Please enter Maximum Charge UOM' id='" + prorateinstanceId + "_MaximumChargeCurrency' name='" + id + ".MaximumChargeCurrency' type='hidden' value=" + ProrataCurrency + " /></div></td></tr>";



Now I have added validations dynamically
JavaScript
$("#" + aProrateinstanceId + "_MaximumCharge").rules('add', { requiredProrataValidation: ["#" + aProrateinstanceId + "_MaximumCharge", 'MaximumCharge is required'] });



Till this point the validations are working properly.But a requirement came like,few validations should be disabled when I select a particular value from drop down and few should work.

I have tried to achieve this
1.by disabling the input text boxes for that particular condition
2.by applying display :none for those textboxes
3.by removing data-val attribute for those textboxes
4.by removing data-val-required attribute .

But still this is applying validations for this condition.

So my main concern is,as I have applied the validations dynamically,But I need help/guidance to remove these applied validations for this drop down change event.

JavaScript
if (selectedvalue == 'Flat') {
        $('#tdMaxCharge').hide();
        $('#tdMaxCharge').find('input').each(function(){$(this).attr('disabled','disabled') })

    }
Posted
Comments
Sinisa Hajnal 6-Oct-14 6:19am    
Since you have .rules('add'...) why can't you use rules('remove'...there is probably a way from within your validation library.

Also, if the rules create client side validator functions or objects, you could probably remove those objects instead of textboxes.
Mahesh Pachbhai 7-Oct-14 5:26am    
Hi
I have tried by using .rules('remove') and it worked fine.
But according to requirement,I needed to remove the HTML for these textboxes as there was autocomplete extenssion methods associated with it for intelligence.

However The solution provided by you also worked.

Thank you very much....!!!!!!!!!!
cheers....:)

Hi Mahesh,

I think you can try with this
rules( 'remove', rules );


Hope this helps.
Thanks
 
Share this answer
 
I have tried by using .rules('remove') and it worked fine.
But according to requirement,I needed to remove the HTML for these textboxes as there was autocomplete extenssion methods associated with it for intelligence and I have written generic methods to achieve this removal and generation so job done.

However The solution provided by both of you also worked.

Thanks a lot....

cheers....:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900