.net mvc2框架,简单jquery表单验证

 

  
  
  
  
  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> 
  2.  
  3.  
  4. <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
  5.     YzTest 
  6. </asp:Content> 
  7.  
  8. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
  9. <script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> 
  10. <script type="text/javascript"> 
  11.     $(document).ready(function () { 
  12.         $("#Form1").submit(function () { 
  13.             var a = $("#Email").val(); 
  14.             var validate = true
  15.             alert(a); 
  16.             if (a == "") { 
  17.                 validate = false
  18.                 $("#DEmail").html("对不起,该选项不能为空"); 
  19.             } 
  20.             else { 
  21.                 var reg =/^(0|[1-9]\d*)$/; 
  22.                 alert(reg.test(a)); 
  23.                 if (reg.test(a)) { 
  24.                     if (a > 100 || a < 0) { 
  25.                         alert("a"); 
  26.                         validate = false
  27.                         $("#DEmail").html("年龄范围在0-100之间!"); 
  28.                     } 
  29.                     else { 
  30.                         $("#DEmail").html(""); 
  31.                     } 
  32.                 } 
  33.                 else { 
  34.                     $("#DEmail").html("请输入数字"); 
  35.                     validate = false
  36.                 } 
  37.             } 
  38.             return validate; 
  39.         }); 
  40.     }); 
  41. </script> 
  42.     <h2>验证测试</h2> 
  43.     <%using (Html.BeginForm("YzTest", "Home", FormMethod.Post, new { id = "Form1" })) 
  44.       {%> 
  45.     <table> 
  46.     <tr> 
  47.     <td> 
  48.     (Email验证) 
  49.     </td> 
  50.     <td><input type="text" id="Email"/><div id="DEmail"></div></td> 
  51.     </tr> 
  52.     <tr><td><input type="submit" value="验证" /></td></tr> 
  53.     </table> 
  54.     <%} %> 
  55.      
  56. </asp:Content> 

贴代码。

你可能感兴趣的:(职场,休闲,jquery表单验证)