Introduction to basic usage of jquery.validate.js

Source: Internet
Author: User
Tags valid email address

Jquery.validate.js is a verification plugin under jquery, the function is relatively powerful, has long been heard but one has not been used, now is to be able to study it.

Here reproduced an article written by predecessors, in my own understanding of the revised, only for record.

First put the code of a large domestic company:

JavaScript code
  1. <script type= "text JavaScript" = "" >
  2. function lang (key) {
  3. Mylang = {
  4. ' Ls_input_myb ': ' Please enter your account ',
  5. ' Ls_myb_email ': ' Roaming currency account is an email address ',
  6. ' Ls_login_password ': ' Please enter your login password ',
  7. ' Ls_password_length ': ' Password length is between {0}-{1} bits ',
  8. ' Ls_input_captcha ': ' Please enter the verification code ',
  9. ' Ls_captcha_length ': ' The length of the verification code is {0} bits ',
  10. ' Ls_account_email ': ' Account name is email address ',
  11. ”:”
  12. };
  13. return Mylang[key];
  14. }
  15. "Script"
  16. <script type= "text JavaScript" = "" >
  17. $ (document). Ready (function () {
  18. $ ("#loginForm"). Validate ({
  19. Rules: {
  20. Uemail: {
  21. Required:true,
  22. Email:true
  23. },
  24. Upassword: {
  25. Required:true,
  26. Rangelength: [6, 30]
  27. }
  28. },
  29. Messages: {
  30. Uemail: {
  31. Required:lang (' Ls_input_myb '),
  32. Email:lang (' Ls_myb_email ')
  33. },
  34. Upassword: {
  35. Required:lang (' Ls_login_password '),
  36. Rangelength: $.format (Lang (' Ls_password_length '))
  37. }
  38. },
  39. Errorplacement:function (Error, Element) {
  40. var placement = $ (element.parent ("TD"). Parent ("TR"). Next ("tr"). Find ("TD"). Get (1));
  41. Placement.text (");
  42. Error.appendto (placement);
  43. },
  44. Onkeyup:false
  45. });
  46. var accounttipstext = lang (' Ls_account_email ');
  47. $ ("#uEmail"). focus (function () {
  48. if (!$ (). Parent (). Parent (). Next (). Find (' TD '). Get (1)). Text ()) {
  49. $ (this). Parent (). Parent (). Next (). Find (' TD '). Get (1)). HTML (' <span class= ' font_888_8″> ' + Accounttipstext + ' );
  50. }
  51. $ (this). CSS (' color ', ' #000 ');
  52. }). focus ();
  53. });
  54. "Script"

I just started with this example, in fact this example almost contains the essence of jquery.validate.js, if you fully understand the code is basically a primer.

Think of the past to do the online simulation of the Futures Web page when they write their own code to judge, the real naïve dead ...

The following is a complete article introduction.

Default validation rules
(1) Required:true must lose field
(2) Remote: "check.php" using Ajax method call check.php Validate input value
(3) Email:true must enter the correct format of e-mail
(4) Url:true must enter the correct format of the URL
(5) Date:true must enter the correct format date
(6) Dateiso:true must enter the correct format for the date (ISO), for example: 2009-06-23, 1998/01/22 only verify the format, do not verify the validity
(7) Number:true must enter a valid number (negative, fractional)
(8) Digits:true must enter an integer
(9) CreditCard: Must enter a valid credit card number
Equalto: The "#field" input value must be the same as #field
(one) Accept: Enter a string with a valid suffix (the suffix of the uploaded file)
Maxlength:5 input string with a maximum length of 5 (Chinese characters are counted as one character)
(minlength:10) Enter a string with a minimum length of 10 (Chinese characters are counted as one character)
(rangelength:[5,10] Enter a string that must be between 5 and 10 in length) (Chinese characters are counted as one character)
(range:[5,10] Input value must be between 5 and 10
(+) Max:5 input value cannot be greater than 5
(min:10) input value cannot be less than 10
The default hints
Messages: {
Required: "This field is required.",
Remote: "Please fix this field.",
Email: "Please enter a valid email address."
URL: "Please enter a valid URL.",
Date: "Please enter a valid date.",
Dateiso: "Please enter a valid date (ISO).",
Datede: "Bitte geben Sie ein G eyebrow ltiges Datum ein.",
Number: "Please enter a valid number.",
Numberde: "Bitte geben Sie eine Nummer ein.",
Digits: "Please enter only digits",
CreditCard: "Please enter a valid credit card number.",
Equalto: "Please enter the same value again.",
Accept: "Please enter a value with a valid extension.",
MaxLength: $.validator.format ("Please enter no more than {0} characters."),
MinLength: $.validator.format ("Please enter at least {0} characters."),
Rangelength: $.validator.format ("Please enter a value between {0} and {1} characters long."),
Range: $.validator.format ("Please enter a value between {0} and {1}."),
Max: $.validator.format ("Please enter a value of less than or equal to {0}."),
Min: $.validator.format ("Please enter a value greater than or equal to {0}.")
},
If you need to modify, you can add in the JS code:
Jquery.extend (JQuery.validator.messages, {
Required: "Required field",
Remote: "Please fix this field",
Email: "Please enter the correct format of e-mail",
URL: "Please enter a valid URL",
Date: "Please enter a valid date",
Dateiso: "Please enter a valid date (ISO).",
Number: "Please enter a valid digit",
Digits: "Can only enter integers",
CreditCard: "Please enter a valid credit card number",
Equalto: "Please enter the same value again",
Accept: "Please enter a string with a valid suffix name",
Maxlength:jQuery.validator.format ("Please enter a string with a maximum length of {0}"),
Minlength:jQuery.validator.format ("Please enter a string with a minimum length of {0}"),
Rangelength:jQuery.validator.format ("Please enter a string between {0} and {1}"),
Range:jQuery.validator.format ("Please enter a value between {0} and {1}"),
Max:jQuery.validator.format ("Please enter a value of {0} max"),
Min:jQuery.validator.format ("Please enter a value of minimum {0}")
});
Recommended practice, put this file into messages_cn.js and introduce it in the page

CLS CC NET Investment International

Go to http://www.php1.cn/detail/jquery-validate-js_DeJiBenYongFaRuMen.html

Introduction to basic usage of jquery.validate.js

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.