I am having two forms for example login and register.
In login page i am having two fields nam开发者_StackOverflowely username and password.
In register page i am having fields namely firstname, lastname, username, dob, and password.
Here we can able to validate the above forms using two different javascript methods.
My question is :
Is that possible to do using a single javascript method?
Kindly advice. Thanks in advance
Yes you can.
If you use some kind of a library that will do that. But your forms will have to be configured for those libraries either with another Javascript or additional HTML Element Attributes. Choosing a library greatly depends on other technologies/libraries/platforms you use. In Asp.net MVC you can easily use xVal for instance. Coupled with data annotations.
Implementation suggestion
You could of course create your own. Something like required could easily be done. ALl form fields that are required can have an attribute required=true
. You could then just go throu all fields and check this custom attribute and act appropriately.
This would make a universal form validation easily possible.
If you are using jQuery, I can recommend the following plugin http://bassistance.de/jquery-plugins/jquery-plugin-validation/
If you're not using jQuery, you should be :)
精彩评论