Plugins_Validation - jQuery JavaScript Library | J Query | Method (Computer Programming)

Please download to get full document.

View again

of 7
0 views
PDF
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Document Description
jQueryPluginsUIM eetupsForumBlogAboutDonate Download Documentation Tutorials Bug Tracker Discussion DOCUMENTATION Search jQuery GETTING STARTED M ain Page Downloading jQuery How jQuery Works FAQ Tutorials Using jQuery with Other Libraries Variable Types Plugins/Validation From jQuery JavaScript Library « Back to the jQuery Validation Plugin page API REFERENCE jQuery Core Selectors Attributes Traversing M anipulation CSS Events Effects Ajax Utilities jQuery UI CONTENTS 1 Validate forms lik
Document Share
Document Tags
Document Transcript
  1/7docs.jquery.com/Plugins/Validation GETTING STARTEDAPI REFERENCEPLUGINSSUPPORTABOUT JQUERYTOOLBOXVIEWSPERSONAL TOOLS jQueryPluginsUIMeetupsForumBlogAboutDonate DownloadDocumentationTutorialsBug TrackerDiscussion DOCUMENTATION Search jQuery    Main PageDownloading jQueryHow jQuery WorksFAQ TutorialsUsing jQuery with OtherLibrariesVariable TypesjQuery CoreSelectorsAttributesTraversingManipulationCSSEventsEffectsAjaxUtilitiesjQuery UIPlugin RepositoryAuthoringMailing List and ChatSubmit New BugCommercial SupportContributorsHistory of jQuerySites Using jQueryBrowser CompatibilityLicensingDonateWhat links hereRelated changesUpload fileSpecial pagesPrintable versionPermanent linkArticleDiscussionEditHistoryLog in / create account Plugins/Validation From jQuery JavaScript Library « Back to the jQuery Validation Plugin page CONTENTS 1 Validate forms like you've never been validating before!2 Example2.1 Isn't that nice and easy?2.2 A few things to look for when playing around with the demo3 API Documentation3.1 Options for the validate() method3.2 Plugin methods3.3 Custom selectors3.4 Utilities3.5 Validator3.6 List of built-in Validation methods4 General Guidelines4.1 Fields with complex names (brackets, dots)4.2 Too much recursion5 Demos5.1 The Marketo sign-up form5.1.1 The Marketo sign-up form, step 25.2 The Remember The Milk sign-up form5.3 A multipart buy&sell a house form5.4 Using remote validation to help with captchas6 Featured Sites using the plugin VALIDATE FORMS LIKE YOU'VE NEVER BEEN VALIDATINGBEFORE! But doesn't jQuery make it so very easy to write your own validation plugin? Sure, but therestill are a lot of subtleties that you have to worry about: You need a standard library of validationmethods (think of emails, URLs, credit card numbers). You need to place error messages in theDOM and show and hide them when appropriate. You want to react to more than just a submitevent, like keyup and blur. You may need different ways to specify validation rules, based on theserver-side enviroment you are using on different projects. And after all, you don't want to reinventthe wheel, do you? But aren't there already a ton of validation plugins out there? Right, there are a lot of non-jQuery-based solutions (which you'd avoid since you found jQuery) and some jQuery-basedsolutions. This particular one you are looking at is one of the oldest jQuery plugins (started in July2006) and proved itself in projects all around the world. There is also an article discussing how thisplugin fits the bill of the should-be validation solution.Not convinced? Have a look at this example: EXAMPLE DemoView SourceValidating a comment form. $( #commentForm ).validate();  2/7docs.jquery.com/Plugins/Validation <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd ><html><head><script src= http://code.jquery.com/jquery-latest.js ></script><script type= text/javascript src= http://jzaefferer.github.com/jquery-validat<style type= text/css >* { font-family: Verdana; font-size: 96%; }label { width: 10em; float: left; }label.error { float: none; color: red; padding-left: .5em; vertical-align: top; }p { clear: both; }.submit { margin-left: 12em; }em { font-weight: bold; padding-right: 1em; vertical-align: top; }</style><script>$(document).ready(function(){$( #commentForm ).validate();});</script> </head><body> <form class= cmxform id= commentForm method= get action= ><fieldset><legend>A simple comment form with submit validation and default messages</leg<p><label for= cname >Name</label><em>*</em><input id= cname name= name size= 25 class= required minlength</p><p><label for= cemail >E-Mail</label><em>*</em><input id= cemail name= email size= 25 class= required email </p><p><label for= curl >URL</label><em> </em><input id= curl name= url size= 25 class= url value= /></p><p><label for= ccomment >Your comment</label><em>*</em><textarea id= ccomment name= comment cols= 22 class= required </p><p><input class= submit type= submit value= Submit /></p></fieldset></form></body></html> Isn't that nice and easy? A single line of jQuery to select the form and apply the validation plugin. And a bit of metadata oneach element to specify the validation rules.Of course that isn't the only way to specify rules. You also don't have to rely on those defaultmessages, but they come in handy when starting to setup validation for a form. A few things to look for when playing around with the demo After trying to submit an invalid form, the first invalid element is focused, allowing the user tocorrect the field. If another invalid field, that wasn't the first one, was focused before submit, thatfield is focused instead, allowing the user start at the bottom, if he prefers that.Before a field is marked as invalid, the validation is lazy: Before submitting the form for the firsttime, the user can tab through fields without getting annoying messages - he won't get buggedbefore he had the chance to actually enter a correct valueOnce a field was marked invalid, it is eagerly validated: As soon as the user entered the necessaryvalue, the error message is removedIf the user enters something in a non-marked field, and tabs/clicks away from it (blur the field), itis validated - obviously the user had the intention to enter something, but failed to enter thecorrect valueThat behaviour can be irritating when clicking through demos of the validation plugin - it isdesigned for an unobtrusive user experience, annoying the user as little as possible withunnecessary error messages. So when you try out other demos, try to react like one of your userswould, and see if the behaviour is better then. If not, please let me know about any ideas you mayhave for improvements! API DOCUMENTATION You're likely looking for Options for the validate() method If not, read on.  3/7docs.jquery.com/Plugins/Validation Throughout the documentation, two terms, that you need to know about and their meaning in thecontext of the validation plugin, are used very often: method : A validation method implements the logic to validate an element, like an email methodthat checks for the right format of an text input's value. A set of standard methods is available, andit is easy to write your own. rule : A validation rule associates an element with a validation method, like validate input withname primary-mail with methods required and email .For a start, the validate-method: Plugin methods NameType   Plugin methods: NameTypevalidate( options )Returns: ValidatorValidates the selected form. valid( )Returns: BooleanChecks whether the selected form is valid or whether all selected elements are valid. rules( )Returns: OptionsReturn the validations rules for the first selected element. rules( add , rules )Returns: OptionsAdds the specified rules and returns all rules for the first matched element. Requires that theparent form is validated, that is, $( form ).validate() is called first. rules( remove , rules )Returns: OptionsRemoves the specified rules and returns all rules for the first matched element. removeAttrs( attributes )Returns: OptionsRemove the specified attributes from the first matched element and return them. Custom selectors NameType   Custom selectors: NameType:blankReturns: Array<Element>Matches elements with a blank value :filledReturns: Array<Element>Matches elements with a value. :uncheckedReturns: Array<Element>Matches all elements that are unchecked. Utilities NameType   String utilities: NameType jQuery.validator.format( template, argument, argumentN... )Returns: StringReplaces {n} placeholders with arguments. Validator The validate method returns a Validator object that has a few public methods that you can usetrigger validation programmatically or change the contents of the form. The validator object hasmore methods, but only those documented here are intended for usage.  4/7docs.jquery.com/Plugins/Validation NameType   Validator methods: NameTypeform( )Returns: BooleanValidates the form, returns true if it is valid, false otherwise. element( element )Returns: BooleanValidates a single element, returns true if it is valid, false otherwise. resetForm( )Returns: undefinedResets the controlled form. showErrors( errors )Returns: undefinedShow the specified messages. numberOfInvalids( )Returns: IntegerReturns the number of invalid fields.There are a few static methods on the validator object: NameType   Validator functions: NameTypesetDefaults( defaults )Returns: undefinedModify default settings for validation. addMethod( name, method, message )Returns: undefinedAdd a custom validation method. It must consist of a name (must be a legal javascript identifier), ajavascript based function and a default string message. addClassRules( name, rules )Returns: undefinedAdd a compound class method - useful to refactor common combinations of rules into a single class. addClassRules( rules )Returns: undefinedAdd compound class methods - useful to refactor common combinations of rules. List of built-in Validation methods A set of standard validation methods is provided: NameType Methods:
Search Related
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks