简单的表单验证插件(Jquery写的)

时间:2022-11-28 14:40:09

     在做web开发的时候经常遇到表单验证问题,表单验证一般有客户端验证和服务器端验证,这个验证插件仅仅能满足我的项目中的基本需求的。

    Validate_Tools.js

  
 
 
  1. function Validate_Text(obj) {  
  2.  
  3.     return $.trim(obj.value) != "";  
  4. }  
  5.  
  6. function Validate_Select(obj) {  
  7.       
  8.     return $.trim(obj.value) != "";  
  9. }  
  10.  
  11. function Validate_List(obj){  
  12.     var flag = false;  
  13.                       
  14.     $(obj).find("input").each(function(){  
  15.         if(this.checked){  
  16.             flag = true;  
  17.             return false;  
  18.         }  
  19.     });  
  20.  
  21.     return flag;  
  22. }  
  23.  
  24. function Validate_Expression(objValue, reg){  
  25.     return $.trim(objValue) == "" ? false : new RegExp(reg).test(objValue);  
  26. }  
  27.  
  28. function Validate_Obj(obj) {  
  29.     var flag = false;  
  30.     var errorMsg = "";  
  31.     var objType = $(obj).attr("type");  
  32.     var objTitle = $(obj).parent(0).prev().text().replace(":""").replace(":""");  
  33.  
  34.     try{  
  35.         if(objType == "text" || objType == "textarea" || objType == "password"){  
  36.             var validateType = $(obj).attr("ValidateType");  
  37.             switch (validateType){  
  38.                 case "Int":  
  39.                     flag = Validate_Expression(obj.value, "^[0-9]*$");  
  40.                     if (!flag) {  
  41.                         if ($.trim(obj.value) == "") {  
  42.                             errorMsg = objTitle + "不能为空!";  
  43.                         }  
  44.                         else {  
  45.                             errorMsg = objTitle + "格式有误,请填写正确的格式!";  
  46.                         }  
  47.                     }  
  48.                     break;  
  49.                 case "Float":  
  50.                     flag = Validate_Expression(obj.value, "^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$");  
  51.                     if (!flag) {  
  52.                         if ($.trim(obj.value) == "") {  
  53.                             errorMsg = objTitle + "不能为空!";  
  54.                         }  
  55.                         else {  
  56.                             errorMsg = objTitle + "格式有误,请填写正确的格式!";  
  57.                         }  
  58.                     }  
  59.                     break;  
  60.                 case "Email":  
  61.                     flag = Validate_Expression(obj.value, "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$");  
  62.                     if (!flag) {  
  63.                         if ($.trim(obj.value) == "") {  
  64.                             errorMsg = objTitle + "不能为空!";  
  65.                         }  
  66.                         else {  
  67.                             errorMsg = objTitle + "格式有误,请填写正确的邮件格式!";  
  68.                         }  
  69.                     }  
  70.                     break;  
  71.                 default:  
  72.                     var regularExpression = $(obj).attr("ValidateExpression");  
  73.                     if (regularExpression != undefined && regularExpression != "") {  
  74.                         flag = Validate_Expression(obj.value, regularExpression);  
  75.                         if (!flag) {  
  76.                             if ($.trim(obj.value) == "") {  
  77.                                 errorMsg = objTitle + "不能为空!";  
  78.                             }  
  79.                             else {  
  80.                                 errorMsg = objTitle + "格式有误!";  
  81.                             }  
  82.                         }  
  83.                     }  
  84.                     else {  
  85.                         flag = Validate_Text(obj);  
  86.                         if (!flag) {  
  87.                             errorMsg = objTitle + "不能为空!";  
  88.                         }  
  89.                     }  
  90.                     break;  
  91.             }  
  92.         }  
  93.         else if(objType == "select-one"){  
  94.             flag = Validate_Select(obj);  
  95.             if (!flag) {   
  96.                 errorMsg = "请选择" + objTitle + "!";  
  97.             }  
  98.         }  
  99.         else if(objType == "file"){  
  100.             flag = Validate_Text(obj);  
  101.             if (!flag) {  
  102.                 errorMsg = "请选择上传文件" + objTitle + "!";  
  103.             }  
  104.         }  
  105.         else{  
  106.             flag = Validate_List(obj);  
  107.             if (!flag) {  
  108.                 errorMsg = "请选择" + objTitle + "!";  
  109.             }  
  110.         }  
  111.           
  112.         if(!flag){  
  113.             if($(obj).attr("ErrorMsg") != undefined && $(obj).attr("ErrorMsg") != ""){  
  114.                 errorMsg = $(obj).attr("ErrorMsg");  
  115.             }  
  116.               
  117.             alert(errorMsg);  
  118.             try{  
  119.                 obj.focus();  
  120.             }  
  121.             catch(e){  
  122.             }  
  123.             return flag;  
  124.         }  
  125.     }  
  126.     catch(e){  
  127.         alert(e.description);  
  128.         flag = false;  
  129.         return flag;  
  130.     }  
  131.       
  132.     return flag;  
  133. }  
  134.  
  135. function Validate_Form(){  
  136.     var flag = true;  
  137.  
  138.     try {  
  139.         $("*[ValidateType]").each(function () {  
  140.             flag = Validate_Obj(this);  
  141.  
  142.             if (!flag) {  
  143.                 return flag;  
  144.             }  
  145.         });  
  146.     }  
  147.     catch (e) {  
  148.         alert(e.description);  
  149.         flag = false;  
  150.     }  
  151.       
  152.     return flag;  
  153. }  
  154.  
  155. function Validate_Group(group) {  
  156.     var flag = true;  
  157.  
  158.     try {  
  159.         $("*[ValidateGroup]").each(function () {  
  160.             if ($(this).attr("type") != "submit") {  
  161.                 if ($(this).attr("ValidateGroup") == group) {  
  162.                     flag = Validate_Obj(this);  
  163.  
  164.                     if (!flag) {  
  165.                         return flag;  
  166.                     }  
  167.                 }  
  168.             }  
  169.         });  
  170.     }  
  171.     catch (e) {  
  172.         alert(e.description);  
  173.         flag = false;  
  174.     }  
  175.  
  176.     return flag;  
  177. }  
  178.  
  179. $(function () {  
  180.     $("input[type='submit']").each(function () {  
  181.         if ($(this).attr("ValidateGroup") != undefined && $(this).attr("ValidateGroup") != "") {  
  182.             $(this).click(function () {  
  183.                 return Validate_Group($(this).attr("ValidateGroup"));  
  184.             });  
  185.         }  
  186.     });  
  187.     //添加必填提示  
  188. //    $("*[ValidateType]").each(function () {  
  189. //        if ($(this).attr("type") != "submit") {  
  190. //            $(this).parent(0).append("<span style='color:red'>*</span>");  
  191. //        }  
  192. //    });  
  193. }); 

对于对象type为text ,textarea,password的input标签可以用的验证类型ValidateType为:Int,Float,Email;

如果需要自定义错误提示信息:可以给标签添加ErrorMsg属性

表单验证要求验证属于该表单的HTML标签,给属于同一个表单的标签添加ValidateGroup属性,submit按钮也需要添加ValidateGroup,要求同一表单中的input标签和submit 按钮的ValidateGroup属性值相同

用法如下面的代码:

  
 
 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ProductQuotationEdit.aspx.cs" Inherits="Trade.Web.Product.ProductQuotationEdit" %> 
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3. <html xmlns="http://www.w3.org/1999/xhtml"> 
  4. <head runat="server"> 
  5.     <title>供应商报价</title> 
  6.     <link href="../Styles/StyleSheet.css" rel="stylesheet" type="text/css" /> 
  7.     <link rel="stylesheet" href="../Styles/AutoComplete/jquery.autocomplete.css" type="text/css" /> 
  8.     <script type="text/javascript" src="../Scripts/jquery-1.4.4.min.js"></script> 
  9.     <script type="text/javascript" src="../Scripts/Validate/Validate_Tools.js"></script>      
  10.     <script src="../Scripts/AutoComplete/jquery.autocomplete.min.js" type="text/javascript"></script> 
  11.     <script type="text/javascript"> 
  12.         function returnValue() {  
  13.             try {  
  14.                 parent.closeDiv();  
  15.             }  
  16.             catch (e) {  
  17.                 alert(e.message);  
  18.             }  
  19.         }  
  20.     </script> 
  21. </head> 
  22. <body> 
  23.     <form id="form1" runat="server"> 
  24.     <div> 
  25.         <table class="table_edit"> 
  26.             <tr> 
  27.                 <th> 
  28.                     供应商:  
  29.                 </th> 
  30.                 <td> 
  31.                     <asp:TextBox ID="ID" runat="server" Visible="false"></asp:TextBox> 
  32.                     <asp:TextBox ID="ProductID" runat="server" Visible="false"></asp:TextBox> 
  33.                 </td> 
  34.                 <th> 
  35.                     报价日期:  
  36.                 </th> 
  37.                 <td> 
  38.                     <asp:TextBox ID="QuotationDate" runat="server" ValidateGroup="Supplier" ValidateType="Text"></asp:TextBox> 
  39.                 </td> 
  40.             </tr> 
  41.             <tr> 
  42.                 <th> 
  43.                     供应商货号:  
  44.                 </th> 
  45.                 <td> 
  46.                     <asp:TextBox ID="SupplierItemNo" runat="server" ValidateGroup="Supplier" ValidateType="Text"></asp:TextBox> 
  47.                 </td> 
  48.                 <th> 
  49.                     币种:  
  50.                 </th> 
  51.                 <td> 
  52.                     <asp:DropDownList ID="Currency" runat="server" ValidateGroup="Supplier" ValidateType="Text"></asp:DropDownList> 
  53.                 </td> 
  54.             </tr> 
  55.             <tr> 
  56.                 <th> 
  57.                     单价:  
  58.                 </th> 
  59.                 <td> 
  60.                     <asp:TextBox ID="Price" runat="server" ValidateGroup="Supplier" ValidateType="Float"></asp:TextBox> 
  61.                 </td> 
  62.                 <th> 
  63.                     起订量:  
  64.                 </th> 
  65.                 <td> 
  66.                     <asp:TextBox ID="MiniOrderQty" runat="server" ValidateGroup="Supplier" ValidateType="Int"></asp:TextBox> 
  67.                 </td> 
  68.             </tr> 
  69.             <tr> 
  70.                 <th> 
  71.                     是否开票:  
  72.                 </th> 
  73.                 <td> 
  74.                     <asp:DropDownList ID="IsBilling" runat="server" ValidateGroup="Supplier" ValidateType="Text"> 
  75.                         <asp:ListItem Text="是" Value="1"></asp:ListItem> 
  76.                         <asp:ListItem Text="否" Value="0"></asp:ListItem> 
  77.                     </asp:DropDownList> 
  78.                 </td> 
  79.                 <th> 
  80.                     是否含税:  
  81.                 </th> 
  82.                 <td> 
  83.                     <asp:DropDownList ID="IsTax" runat="server" ValidateGroup="Supplier" ValidateType="Text"> 
  84.                         <asp:ListItem Text="是" Value="1"></asp:ListItem> 
  85.                         <asp:ListItem Text="否" Value="0"></asp:ListItem> 
  86.                     </asp:DropDownList> 
  87.                 </td> 
  88.             </tr> 
  89.             <tr> 
  90.                 <th> 
  91.                     备注:  
  92.                 </th> 
  93.                 <td colspan="3"> 
  94.                     <asp:TextBox ID="Remark" runat="server" TextMode="MultiLine"></asp:TextBox> 
  95.                 </td> 
  96.             </tr> 
  97.             <tr> 
  98.                 <td> 
  99.                 </td> 
  100.                 <td> 
  101.                     <asp:Button ID="btnSave" runat="server" Text="保 存" ValidateGroup="Supplier" OnClick="btnSave_Click" /> 
  102.                 </td> 
  103.             </tr> 
  104.         </table> 
  105.     </div> 
  106.     </form> 
  107. </body> 
  108. </html> 

由于jquery1.4.4可以取得select标签的type属性为“select-one”,

但是jquery1.7.1版本获取不到select标签的type属性,所以可以给select添加type="select-one"   。

 

本文出自 “Zero's Blog” 博客,请务必保留此出处http://zerosoft.blog.51cto.com/679447/856748