Unobstructive client side validation for dynamically added input fields

匿名 (未验证) 提交于 2019-12-03 09:18:39

问题:

I followed this excellent post for generating dynamic controls in my mvc3 app.

And now I am trying to do client side unobstructive validation for phonenumber field. So I added 'Required' attribute on the 'PhoneNumber' property.

The unobstructive validation works for the phonenumber fields which are generated/rendered by the server (i.e. on pageload). But it doesnt works for the fields that are dynamically added by javascript method.

I know that I need to write some jquery code to add the rules/adapters or anything which notifies the browser that the dynamically added phonenumber fields needs to be validated. Please help me how to proceed.

Thanks, M

回答1:

You should reparse the validation rules for all dynamically added elements:

$("form").removeData("validator"); $("form").removeData("unobtrusiveValidation"); $.validator.unobtrusive.parse("form"); 


回答2:

the following validation plugin might help you

http://docs.jquery.com/Plugins/Validation

Also the the examples listed at the end of the page to get idea about how to use them effectively...

You need to add the class for the fields you want to validate.....

also you can provide inputmask for phone no, credit card no, etc....



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!