Introduction to jquery. Validate. js

Source: Internet
Author: User

See http://docs.jquery.com/plugins/validationand sort out

 

Jquery. validate. JS is a verification framework of jquery. With the advantages of jquery, we can quickly verify some common input, expand our own verification methods, and provide good support for internationalization.

 

It's easy to use this function. See the following code:

HTML code
  1. <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"
  2. Http://www.w3.org/TR/html4/loose.dtd>
  3. <HTML>
  4. <Head>
  5. <SCRIPT src = "http://code.jquery.com/jquery-latest.js"> </SCRIPT>
  6. <LINK rel = "stylesheet" href = "http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.css" type = "text/CSS" Media = "screen"/>
  7. <SCRIPT type = "text/JavaScript" src = "http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"> </SCRIPT>
  8. <Style type = "text/CSS">
  9. * {Font-family: verdana; font-size: 96% ;}
  10. Label {width: 10em; float: Left ;}
  11. Label. error {float: none; color: red; padding-left:. 5em; Vertical-align: Top ;}
  12. P {clear: Both ;}
  13. . Submit {margin-left: 12em ;}
  14. Em {font-weight: bold; padding-Right: 1em; Vertical-align: Top ;}
  15. </Style>
  16. <SCRIPT>
  17. $ (Document). Ready (function (){
  18. $ ("# Commentform"). Validate ();
  19. });
  20. </SCRIPT>
  21. </Head>
  22. <Body>
  23. <Form class = "cmxform" id = "commentform" method = "get" Action = "">
  24. <Fieldset>
  25. <Legend> A simple comment form with submit validation and default messages </legend>
  26. <P>
  27. <Label for = "cname"> name </label>
  28. <Em> * </em> <input id = "cname" name = "name" size = "25" class = "required" minlength = "2"/>
  29. </P>
  30. <P>
  31. <Label for = "cemail"> email </label>
  32. <Em> * </em> <input id = "cemail" name = "email" size = "25" class = "required email"/>
  33. </P>
  34. <P>
  35. <Label for = "curl"> URL </label>
  36. <Em> </em> <input id = "curl" name = "url" size = "25" class = "url" value = ""/>
  37. </P>
  38. <P>
  39. <Label for = "cComment"> your comment </label>
  40. <Em> * </em> <textarea id = "cComment" name = "comment" Cols = "22" class = "required"> </textarea>
  41. </P>
  42. <P>
  43. <Input class = "Submit" type = "Submit" value = "Submit"/>
  44. </P>
  45. </Fieldset>
  46. </Form>
  47. </Body>
  48. </Html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"                     "http://www.w3.org/TR/html4/loose.dtd">

To sum up, we only need to add the following JavaScript code to add the specified form for verification.

HTML code
  1. $ (Document). Ready (function (){
  2. $ ("# Commentform"). Validate ();
  3. });
 $(document).ready(function(){    $("#commentForm").validate();  });

 
However, we also need to add required to the class in the input to be verified. The other is to verify the relevant data. For example, email is the data structure of the verification email.

The following lists the default verification items provided by validate.

 

Required: "required fields ",
Remote: "Please correct this field ",
Email:
"Enter an email in the correct format ",
URL: "enter a valid URL ",
Date: "enter a valid date ",

Dateiso: "enter a valid date (ISO ).",
Number: "enter a valid number ",

Digits: "Only integers can be entered ",
Creditcard: "enter a valid credit card number ",
Similar:
"Enter the same value again ",
Accept: "enter a string with a valid suffix ",
Maxlength:
Jquery. Format ("enter a string with a maximum length of {0 "),
Minlength:
Jquery. Format ("enter a string with at least {0} length "),
Rangelength:
Jquery. Format ("enter a string between {0} and {1 "),
Range:
Jquery. Format ("enter a value between {0} and {1 "),
MAX: jquery. Format ("Enter the maximum value
{0} value "),
Min: jquery. Format ("enter a minimum value of {0 ")

 

Validate() Option Debug: debug mode
$(".selector").validate



({
debug: true
})

Set debugging as default

 

$.validator.setDefaults



({
debug: true
})

 

Submithandler: use other methods to replace the default submit, such as Ajax-based submission.

 

$(".selector").validate



({
submitHandler: function(form) {
$(form).ajaxSubmit();
}
})

 

Ignore: Ignore some elements not verified

$("#myform").validate



({
ignore: ".ignore"
})

 

Rules: by default, according to the form classes, attributes,
Metadata judgment, but it can also be declared in the validate Function
Key/value can be customized rules. Key is the name of an object. value is the rule of an object and can be used in combination.
Class/attribute/metadata rules.

The following code specifically verifies that name = 'name' In the selector class is required and email is both required and conforms to the email format

 

$(".selector").validate

({
rules: {
// simple rule, converted to {required:true}
name: "required",
// compound rule
email: {
required: true,
email: true
}
}
})

 

Messages: Change the default message.

 

$(".selector").validate

({
rules: {
name: "required",
email: {
required: true,
email: true
}
},
messages: {
name: "Please specify your name",
email: {
required: "We need your email address to contact you",
email: "Your email address must be in the format of name@domain.com"
}
}
})

Groups: defines a group, puts the error information consent in several places in one place, and uses errorplacement to control where the error information is put

 

$("#myform").validate
({
groups: {
username: "fname lname"
},
errorPlacement: function(error, element) {
if (element.attr("name") == "fname" || element.attr("name") == "lname" )
error.insertAfter("#lastname");
else
error.insertAfter(element);
},
debug:true
})
  • Jquery.validate.zip
    (211.8 KB)
  • Downloads: 23

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.