注銷

          注銷

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            112 隨筆 :: 7 文章 :: 18 評(píng)論 :: 0 Trackbacks

          .net中客戶端驗(yàn)證可以用微軟自帶的驗(yàn)證控件,但明顯沒有直接寫的Javascript來得簡單有效,請(qǐng)參照以下三步:
          第一:Form如下:

          < form?id = " Form1 " ?method = " post " ?runat = " server " >
          ????????????
          < FONT?face = " 宋體 " ></ FONT >< FONT?face = " 宋體 " ></ FONT >
          ????????????
          < br >
          ????????????
          1 .?Name?: < br >
          ????????????
          < asp:TextBox?ID = " txtName " ?runat = " server " ? />< br >
          ????????????
          2 .?Email?: < br >
          ????????????
          < asp:TextBox?ID = " txtEmail " ?runat = " server " ? />< br >
          ????????????
          3 .?Web?URL?: < br >
          ????????????
          < asp:TextBox?ID = " txtWebURL " ?runat = " server " ? />< br >
          ????????????
          4 .?Zip?: < br >
          ????????????
          < asp:TextBox?ID = " txtZIP " ?runat = " server " ? />< br >
          ????????????
          5 .Content < br >
          ????????????
          < asp:TextBox?ID = " txtContent " ?runat = " server " ?TextMode = " MultiLine " ?Width = " 504px " ?Height = " 80px " ? />
          ????????????
          < br >
          ????????????
          < asp:Button?ID = " btnSubmit " ?OnClientClick = " ?return?validate() " ?runat = " server " ?Text = " Submit " ? />
          ????????
          </ form >

          第二:調(diào)用函數(shù)如下(可自行擴(kuò)展)

          <script?language="javascript"?type="text/javascript">
          ????????????????
          function?validate()
          ????????????????????????
          {
          ????????????????????????????
          if?(document.getElementById("<%=txtName.ClientID%>").value=="")
          ????????????????????????????
          {
          ????????????????????????????????????????alert(
          "Name?Feild?can?not?be?blank");
          ????????????????????????????????????????document.getElementById(
          "<%=txtName.ClientID%>").focus();
          ????????????????????????????????????????
          return?false;
          ????????????????????????????}

          ????????????????????????????
          if(document.getElementById("<%=txtEmail.ClientID?%>").value=="")
          ????????????????????????????
          {
          ????????????????????????????????????????alert(
          "Email?id?can?not?be?blank");
          ????????????????????????????????????????document.getElementById(
          "<%=txtEmail.ClientID?%>").focus();
          ????????????????????????????????????????
          return?false;
          ????????????????????????????}

          ????????????????????????????
          //?var?emailPat?=?/^(".*"|[A-Za-z]w*)@([d{1,3}(.d{1,3}){3}]|[A-Za-z]w*(.[A-Za-z]w*)+)$/;
          ????????????????????????????//var?emailPat?=?"w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*";
          ????????????????????????????//var?emailPat????=????/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
          ????????????????????????????//var?emailPat????=????'^([w]+@([w]+.)+[a-zA-Z]{2,9}(s*;s*[w]+@([w]+.)+[a-zA-Z]{2,9})*)$';
          ????????????????????????????var?emailPat????=????"^[a-zA-Z0-9_.]+@[a-zA-Z0-9-]+[.a-zA-Z]+$";
          ????????????????????????????
          var?emailid=document.getElementById("<%=txtEmail.ClientID?%>").value;
          ????????????????????????????
          var?matchArray?=?emailid.match(emailPat);
          ????????????????????????????
          if?(matchArray?==?null)
          ????????????????????????????
          {
          ????????????????????????????????????alert(
          "Your?email?address?seems?incorrect.?Please?try?again.");
          ????????????????????????????????????document.getElementById(
          "<%=txtEmail.ClientID?%>").focus();
          ????????????????????????????????????
          return?false;
          ????????????????????????????}

          ????????????????????????????
          if(document.getElementById("<%=txtWebURL.ClientID?%>").value=="")
          ????????????????????????????
          {
          ????????????????????????????????????alert(
          "Web?URL?can?not?be?blank");
          ????????????????????????????????????document.getElementById(
          "<%=txtWebURL.ClientID?%>").value="http://"
          ????????????????????????????????????document.getElementById(
          "<%=txtWebURL.ClientID?%>").focus();
          ????????????????????????????????????
          return?false;
          ????????????????????????????}

          ????????????????????????????
          var?Url="^[A-Za-z]+://[A-Za-z0-9-_]+\.[A-Za-z0-9-_%&?/.=]+$"
          ????????????????????????????
          var?tempURL=document.getElementById("<%=txtWebURL.ClientID%>").value;
          ????????????????????????????
          var?matchURL=tempURL.match(Url);
          ????????????????????????????
          if(matchURL==null)
          ????????????????????????????
          {
          ????????????????????????????????????alert(
          "Web?URL?does?not?look?valid");
          ????????????????????????????????????document.getElementById(
          "<%=txtWebURL.ClientID?%>").focus();
          ????????????????????????????????????
          return?false;
          ????????????????????????????}

          ????????????????????????????
          if?(document.getElementById("<%=txtZIP.ClientID%>").value=="")
          ????????????????????????????
          {
          ????????????????????????????????????alert(
          "Zip?Code?is?not?valid");
          ????????????????????????????????????document.getElementById(
          "<%=txtZIP.ClientID%>").focus();
          ????????????????????????????????????
          return?false;
          ????????????????????????????}

          ????????????????????????????
          var?digits="0123456789";
          ????????????????????????????
          var?temp;
          ????????????????????????????
          for?(var?i=0;i<document.getElementById("<%=txtZIP.ClientID?%>").value.length;i++)
          ????????????????????????????
          {
          ????????????????????????????????????temp
          =document.getElementById("<%=txtZIP.ClientID%>").value.substring(i,i+1);
          ????????????????????????????????????
          if?(digits.indexOf(temp)==-1)
          ????????????????????????????????????
          {
          ????????????????????????????????????????????????alert(
          "Please?enter?correct?zip?code");
          ????????????????????????????????????????????????document.getElementById(
          "<%=txtZIP.ClientID%>").focus();
          ????????????????????????????????????????????????
          return?false;
          ????????????????????????????????????}

          ????????????????????????????}

          ????????????????????????????
          var?ContentLength="";
          ????????????????????????????
          if?(document.getElementById("<%=txtContent.ClientID%>").value=="")
          ????????????????????????????
          {
          ????????????????????????????????????alert(
          "Content?is?need!");
          ????????????????????????????????????document.getElementById(
          "<%=txtContent.ClientID%>").focus();
          ????????????????????????????????????
          return?false;
          ????????????????????????????}

          ????????????????????????????
          else?if(document.getElementById("<%=txtContent.ClientID%>").value.length>100)
          ????????????????????????????
          {
          ????????????????????????????????alert(
          "Content?is?Too?Long,and?More?than?100?Chars!");
          ????????????????????????????????????document.getElementById(
          "<%=txtContent.ClientID%>").focus();
          ????????????????????????????????????
          return?false;
          ????????????????????????????}

          ????????????????????????????
          return?true;
          ????????????????????????}

          ????????
          </script>


          第三:Page_Load事件中加入一行:

          private?void?Page_Load(object?sender,?System.EventArgs?e)
          ????????
          {
          ?????????????btnSubmit.Attributes.Add(
          "onclick",?"return?validate()");
          ????????}

          posted on 2006-11-30 09:12 注銷..... 閱讀(414) 評(píng)論(0)  編輯  收藏 所屬分類: .net摘要
          主站蜘蛛池模板: 大兴区| 麦盖提县| 内乡县| 同德县| 祁阳县| 岚皋县| 金沙县| 大埔区| 神农架林区| 浦江县| 北票市| 宁夏| 布拖县| 平安县| 裕民县| 开平市| 洪湖市| 太仆寺旗| 轮台县| 义马市| 台东市| 泸定县| 岱山县| 平南县| 涟水县| 昭觉县| 临潭县| 宿松县| 衡阳市| 顺昌县| 股票| 册亨县| 屏东市| 民乐县| 朝阳市| 侯马市| 务川| 灵武市| 宜兰市| 邻水| 封开县|