jQuery plug-in Validation form Validation

function

1. Verification that the required and length of "name" are at least two digits.
2. Verification that "email" is required and in E-mail format.
3. Verify whether the "web address" is a url.
4. Whether "your comment" needs to be verified.

Result chart


example

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>Get started quickly</title>  
  6. <script type="text/javascript" src="../jquery-1.8/jquery-1.8.0.js"></script>  
  7. <script type="text/javascript" src="../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:lem; vertical-align:top;}  
  15. </style>  
  16. <script type="text/javascript">  
  17.     $(document).ready(function() {  
  18.         $("#commentForm").validate();  
  19.     });  
  20. </script>  
  21. </head>  
  22.   
  23. <body>  
  24.     <form class="cmxform" id="commentForm" method="get" action="#">  
  25.         <fieldset><!--fieldset Elements to group related elements within a form-->  
  26.             <legend>A simple example of verification with verification prompt</legend><!--legend Element is fieldset Element definition title-->  
  27.             <p>  
  28.                 <label for="cusername">full name</label><em>*</em>  
  29.                 <input id="cusername" name="username" size="25" class="required" minlength="2"/>  
  30.             </p>  
  31.             <p>  
  32.                 <label for="cemail">E-mail</label><em>*</em>  
  33.                 <input id="cemail" name="email" size="25" class="required email"/>  
  34.             </p>  
  35.             <p>  
  36.                 <label for="curl">website</label><em> </em>  
  37.                 <input id="curl" name="url" size="25" class="url" value=""/>  
  38.             </p>  
  39.             <p>  
  40.                 <label for="ccomment">Your comments</label><em>*</em>  
  41.                 <textarea id="ccomment" name="comment" cols="22" class="required"></textarea>  
  42.             </p>  
  43.             <p>  
  44.                 <input class="submit" type="submit" value="Submit"/>  
  45.             </p>  
  46.         </fieldset>  
  47.     </form>  
  48. </body>  
  49. </html>  

code analysis

1.jQuery code
  1. <script type="text/javascript" src="../jquery-1.8/jquery-1.8.0.js"></script>  
  2. <script type="text/javascript" src="../jquery.validate.js"></script>  
  3. <script type="text/javascript">  
  4.     $(document).ready(function() {  
  5.         $("#commentForm").validate();  
  6.     });  
  7. </script>  
Lines 1 and 2 import the jQuery class library and Validation plug-in.
Line 4 is used to execute the code in function when the code is loaded
Line 5 is to determine which form needs to be validated.
2. Code the validation rules for different fields, and set the corresponding properties of the fields
class="required" is required
class="required email" is required and conforms to the E-mail format.  
class="url" verify for url format
minlength="2" is the minimum length of 2

Keywords: JQuery Javascript curl

Added by project18726 on Sat, 16 May 2020 18:14:38 +0300