The need for client-side validation--jquery.validator

Source: Internet
Author: User
Tags valid email address

Recently in doing a user registration login page, the data search process found a very good client authentication for the Acura-jquery.validate.
It is a plugin of the famous JavaScript package jquery, in fact it has some other plug-ins should be cool, waiting to learn slowly

Official address: http://bassistance.de/jquery-plugins/jquery-plugin-validation/
jquery User's Manual: Http://jquery.org.cn/visual/cn/index.xml

Development is very simple and straightforward to use.
My Code:

Java code
  1. $ (document). Ready (function () {
  2. /* Set default Properties */
  3. $.validator.setdefaults ({
  4. Submithandler:function (form) {Form.submit ();}
  5. });
  6. Chinese characters two bytes
  7. JQuery.validator.addMethod ("Byterangelength", function (value, element, param) {
  8. var length = Value.length;
  9. For (var i = 0; i < value.length; i++) {
  10. if (value.charcodeat (i) > 127) {
  11. length++;
  12. }
  13. }
  14. return this.optional (element) | |   (length >= param[0] && length <= param[1]);
  15. }, "Make sure the value entered is between 3-15 bytes (2 bytes in one text)");
  16. /* Append a custom validation method */
  17. Identification Number Verification
  18. JQuery.validator.addMethod ("Isidcardno", function (value, Element) {
  19. return this.optional (element) | | Isidcardno (value);
  20. }, "Please enter your ID number correctly");
  21. Character verification
  22. JQuery.validator.addMethod ("UserName", function (value, Element) {
  23. return this.optional (element) | |/^[\u0391-\uffe5\w]+$/.test (value);
  24. }, "User name can only include Chinese characters, English letters, numbers and underscores";
  25. Mobile phone number Verification
  26. JQuery.validator.addMethod ("IsMobile", function (value, Element) {
  27. var length = Value.length;
  28. return this.optional (element) | | (length = = &&/^ (((13[0-9]{1}) | (   15[0-9]{1})) +\d{8}) $/.test (value));
  29. }, "Please fill in your mobile phone number correctly");
  30. Phone number Verification
  31. JQuery.validator.addMethod ("Isphone", function (value, Element) {
  32. var tel =/^ (\d{3,4}-?)?   \d{7,9}$/g;
  33. return this.optional (element) | |   (Tel.test (value));
  34. }, "Please fill in your phone number correctly");
  35. ZIP Code Verification
  36. JQuery.validator.addMethod ("Iszipcode", function (value, Element) {
  37. var tel =/^[0-9]{6}$/;
  38. return this.optional (element) | |   (Tel.test (value));
  39. }, "Please fill in your zip code correctly");
  40. $ (regfrom). Validate ({
  41. /* Set validation rules */
  42. Rules: {
  43. UserName: {
  44. Required: true,
  45. UserName: True,
  46. Byterangelength: [3,]
  47. },
  48. Password: {
  49. Required: true,
  50. MinLength: 5
  51. },
  52. Repassword: {
  53. Required: true,
  54. MinLength: 5,
  55. Equalto: "#password"
  56. },
  57. Question: {
  58. Required: true
  59. },
  60. Answer: {
  61. Required: true
  62. },
  63. Realname: {
  64. Required: true
  65. },
  66. Cardnumber: {
  67. Isidcardno: True
  68. },
  69. Mobilephone: {
  70. IsMobile: True
  71. },
  72. Phone: {
  73. Isphone: True
  74. },
  75. Email: {
  76. Required: true,
  77. Email: true
  78. },
  79. ZipCode: {
  80. Iszipcode:True
  81. }
  82. },
  83. /* SET error message */
  84. Messages: {
  85. UserName: {
  86. Required: "Please fill in the user name",
  87. Byterangelength: "User name must be between 3-15 characters (2 characters in one text)"
  88. },
  89. Password: {
  90. Required: "Please fill in the password",
  91. Minlength:jQuery.format ("input {0}.")
  92. },
  93. Repassword: {
  94. Required: "Please fill in the confirmation password",
  95. Equalto: "two times password input is not the same"
  96. },
  97. Question: {
  98. Required: "Please fill in your password prompt question"
  99. },
  100. Answer: {
  101. Required: "Please fill in your password prompt answer"
  102. },
  103. Realname: {
  104. Required: "Please fill in your real name"
  105. },
  106. Email: {
  107. Required: "Please enter an email address",
  108. Email: "Please enter a valid email address"
  109. }
  110. },
  111. /* Where error message is displayed */
  112. Errorplacement:function (Error, Element) {
  113. Error.appendto (Element.parent ());
  114. },
  115. /* Processing when validation passes */
  116. Success:function (label) {
  117. //Set as text for IE
  118. Label.html (""). AddClass ("checked");
  119. },
  120. /* Do not verify when getting focus */
  121. Focusinvalid: false,
  122. OnKeyUp: false
  123. });
  124. When the input box gets focus, the style setting
  125. $ (' input '). focus (function () {
  126. if ($ (this). Was (": Text") | | $ (This). Is (":p assword"))
  127. $ (this). addclass (' focus ');
  128. if ($ (this). Hasclass (' Have_tooltip ')) {
  129. $ (this). Parent (). Parent ().removeclass ('field_normal '). addclass (' field_focus ');
  130. }
  131. });
  132. Style settings when the input box loses focus
  133. $ (' input '). blur (function () {
  134. $ (this). Removeclass (' focus ');
  135. if ($ (this). Hasclass (' Have_tooltip ')) {
  136. $ (this). Parent (). Parent ().removeclass ('field_focus '). addclass (' field_normal ');
  137. }
  138. });
  139. });


Online information Some people say that it has a conflict with the prototype package, I have not used at the same time, this is not very clear, but I have found a problem:
For the minimum/large length of the verification method, the author may consider that everyone's naming habits are different, at the same time do the minlength and MinLength (MaxLength and MaxLength) methods, which should be possible, but for the user message, can only be defined for minlength (maxlength) to invoke a user-defined message,
Otherwise, it simply calls the package's default message, but the exact reason is not yet known. At the same time, this plugin provides localized messages, but for beginners here, how to use it still needs to be explored!

The need for client-side validation--jquery.validator

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.