If the user enters something in a non-marked field, and tabs/clicks away from it (blur the field), it is validated – obviously the user had the intention to enter something, but failed to enter the correct value.Once a field is marked invalid, it is eagerly validated: As soon as the user has entered the necessary value, the error message is removed.Before a field is marked as invalid, the validation is lazy: Before submitting the form for the first time, the user can tab through fields without getting annoying messages – they won't get bugged before having the chance to actually enter a correct value.If another invalid field – that wasn't the first one – was focused before submit, that field is focused instead, allowing the user to start at the bottom if he or she prefers. After trying to submit an invalid form, the first invalid element is focused, allowing the user to correct the field.link A few things to look out for when playing around with the demo You also don't have to rely on those default messages, but they come in handy when starting to setup validation for a form. Of course that isn't the only way to specify rules. Not convinced? Have a look at this example:Ī single line of jQuery to select the form and apply the validation plugin, plus a few annotations on each element to specify the validation rules. There is also an article discussing how this plugin fits the bill of the should-be validation solution. This particular one is one of the oldest jQuery plugins (started in July 2006) and has proved itself in projects all around the world. Right, there are a lot of non-jQuery-based solutions (which you'd avoid since you found jQuery) and some jQuery-based solutions. "But aren't there already a ton of validation plugins out there?" And after all, you don't want to reinvent the wheel, do you? You may need different ways to specify validation rules according to the server-side enviroment you are using on different projects. You want to react to more than just a submit event, like keyup and blur. You need to place error messages in the DOM and show and hide them when appropriate. Sure, but there are still a lot of subtleties to take care of: You need a standard library of validation methods (such as emails, URLs, credit card numbers). "But doesn't jQuery make it easy to write your own validation plugin?" Link Validate forms like you've never validated before!
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |