<script language="JavaScript">


          //設(shè)置焦點(diǎn)函數(shù) 
          function focusElement(formName, elemName) { 
          var elem = document.forms[formName].elements[elemName]; 
          elem.focus( ); 
          elem.select( ); 



          // 驗(yàn)證下拉框 
          function isChosen(select){ 
          if (select.selectedIndex==0) { 
          alert("請(qǐng)選擇下拉單相應(yīng)欄目."); 
          focusElement(select.form.name,select.name); 
          return false; 
          } else { 
          return true; 


          //不為空函數(shù) 
          function isNotEmpty(elem) { 
          var str=elem.value; 
          if(str==null || str.length==0) { 
          alert("此項(xiàng)不能為空"); 
          focusElement(elem.form.name,elem.name); 
          return false; 
          } else { 
          return true; 




          // 驗(yàn)證是否是數(shù)字 
          function isNumber(elem) { 
          var str=elem.value; 
          var oneDecimal=false; 
          var oneChar=0; 
          str=str.toString( ); 
          for (var i=0; i<str.length; i++) { 
          oneChar=str.charAt(i).charCodeAt(0); 
          // - 
          if (oneChar==45) { 
          if (i==0) { 
          continue; 
          } else { 
          alert("Only the first character may be a minus sign."); 
          setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0); 
          return false; 


          // 小數(shù)點(diǎn) 
          if (oneChar==46) { 
          if (!oneDecimal) { 
          oneDecimal=true; 
          continue; 
          } else { 
          alert("輸入的數(shù)字只允許有一個(gè)小數(shù)點(diǎn)."); 
          setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0); 
          return false; 


          // 數(shù)字只能在0和9之間 
          if (oneChar<48 || oneChar > 57) { 
          alert("此項(xiàng)只能輸入數(shù)字."); 
          setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0); 
          return false; 


          return true; 

          // 驗(yàn)證是否是數(shù)字電話 
          function istel(elem) { 
          var str=elem.value; 
          var oneDecimal=false; 
          var oneChar=0; 


          str=str.toString( ); 
          for (var i=0; i<str.length; i++) { 
          oneChar=str.charAt(i).charCodeAt(0); 
          if(oneChar==45){continue;} 
          if(oneChar<48 || oneChar > 57) { 
          alert("此項(xiàng)只能輸入數(shù)字和'-'號(hào)."); 
          setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0); 
          return false; 


          return true; 

          //驗(yàn)證輸入數(shù)據(jù)的長(zhǎng)度 
          //郵編 
          function isLenMatch(elem,lengthNum) { 
          var str=elem.value; 
          if (str.length != lengthNum) { 
          alert("此項(xiàng)內(nèi)容長(zhǎng)度只能為"+lengthNum+"位."); 
          setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0); 
          return false; 
          } else { 
          return true; 




          //郵件地址驗(yàn)證 
          function isEMailAddr(elem) { 
          var str = elem.value; 
          var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/; 
          if (!str.match(re)) { 
          alert("您輸入的不是有效的e-mail地址."); 
          setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0); 
          return false; 
          } else { 
          return true; 


          //單選按鈕驗(yàn)證 
          function isValidRadio(radio) { 
          var valid=false; 
          for (var i=0; i<radio.length; i++) { 
          if (radio.checked) { 
          return true; 


          alert("單選按鈕未選中."); 
          radio[0].focus(); 
          //setTimeout("focusElement('" + radio[0].form.name + "', '" + radio[0].name + "')", 0); 
          focusElement(radio[0].form.name,radio[0].name); 
          return false; 



          </script>


          <script language="JavaScript"> 
          function validateForm(form) { 
          if (isNotEmpty(form.name1) && isNotEmpty(form.name2)) {;}else{return false; 

          </script> 
          <title>JS驗(yàn)證程序Sample</title></head> 


          <body> 
          <form method="GET" action="index.htm" name="sampleForm" onsubmit="return validateForm(this)"> 
          <p>姓名: 
          <input type="text" size="30" name="name1" id="name1" /> 
          </body> 
          </html> 


          表單驗(yàn)證 很有用 
          長(zhǎng)度限制
          <script>
          function test() 
          {
          if(document.a.b.value.length>50)
          {
          alert("不能超過50個(gè)字符!");
          document.a.b.focus();
          return false;
          }
          }
          </script>
          <form name=a onsubmit="return test()">
          <textarea name="b" cols="40" wrap="VIRTUAL" rows="6"></textarea>
          <input type="submit" name="Submit" value="check">
          </form>

          2 只能是漢字 
          <input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')">

          3 只能是英文
          <script language=javascript>
          function onlyEng()
          {
          if(!(event.keyCode>=65&&event.keyCode<=90))
          event.returnvalue=false;
          }
          </script>

          <input onkeydown="onlyEng();">

          4 只能是數(shù)字
          <script language=javascript>
          function onlyNum()
          {
          if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)))
          //考慮小鍵盤上的數(shù)字鍵
          event.returnvalue=false;
          }
          </script>

          <input onkeydown="onlyNum();">

          5 只能是英文字符和數(shù)字
          <input onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">

          6 驗(yàn)證油箱格式
          <SCRIPT LANGUAGE=javascript RUNAT=Server>
          function isEmail(strEmail) {
          if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
          return true;
          else
          alert("oh");
          }
          </SCRIPT>
          <input type=text onblur=isEmail(this.value)>

          7 屏蔽關(guān)鍵字(這里屏蔽sex和****)
          <script language="javascript1.2">
          function test() {
          if((a.b.value.indexOf ("sex") == 0)||(a.b.value.indexOf ("****") == 0)){
          alert("");
          a.b.focus();
          return false;}
          }
          </script>
          <form name=a onsubmit="return test()">
          <input type=text name=b>
          <input type="submit" name="Submit" value="check">
          </form>

          8 兩次輸入密碼是否相同
          <FORM METHOD=POST ACTION="">
          <input type="password" id="input1">
          <input type="password" id="input2">
          <input type="button" value="test" onclick="check()">
          </FORM>
          <script>
          function check()

          with(document.all){
          if(input1.value!=input2.value)
          {
          alert("false")
          input1.value = "";
          input2.value = "";
          }
          else document.forms[0].submit();
          }
          }
          </script>
           
           
          網(wǎng)頁(yè)表單的javascript集成驗(yàn)證方法舉例 
           
                  原理:
                     表單驗(yàn)證無非是要對(duì)要收集每一條信息進(jìn)行驗(yàn)證,也就是要寫一個(gè)名為
                 frmValid的javascript函數(shù),在其中執(zhí)行如下操作:
                       ...
                       if (待驗(yàn)證條目 不符合條件)
                       {
                            alert('出錯(cuò)了!');
                            待驗(yàn)證條目.focus();
                            return false;
                       }
                       ...
                       // all right
                       return true;
                     當(dāng)然,<form ... onsubmit='return frmValid()'>必須包含在
                 HTML代碼中。想想看,待驗(yàn)證條目越多代碼越長(zhǎng),也就越容易出錯(cuò)。
                     下面我們把驗(yàn)證條目放到一個(gè)數(shù)組里,如下:
                       elemArray = new Array(
                           '待驗(yàn)證條目名',
                           '驗(yàn)證條件',
                           '出錯(cuò)提示');
                     那么驗(yàn)證代碼將大大精簡(jiǎn),我們只要如下使用循環(huán)就可實(shí)現(xiàn)上述冗長(zhǎng)
                 代碼時(shí)下的功能,這里我們用with和eval語句構(gòu)造判斷條件:
                       with(eval('obj.'+elems[0]))
                       {
                           if(eval(elems[1]))
                           {
                               window.alert(elems[2]);
                               focus();
                               return false;
                           }
                       }
                     我們建立多位數(shù)組就可實(shí)現(xiàn)循環(huán)遍歷每個(gè)條目:
                       elems = new Array(
                           new Arrary( ...),
                           ...
                       );
                       for(i = 0; i < elems.length; i++)
                       {
                           // 上面的驗(yàn)證語句
                       }

              
              實(shí)戰(zhàn):
                  1、使用如下例子編寫驗(yàn)證腳本:
          <SCRIPT LANGUAGE="javascript" type="text/javascript">
          //
          // Function: frmValid
          // ------------------
          // Author    hongz
          // Usage:    YourForm.onsbumit="return frmValid(this)".
          // Purpose:  To validate form elements in an integrated way.
          //
          function frmValid(obj)
          {
              // Elements array, initialization for validation
              elems = new Array(
                  new Array(
                      'username',  // name of elements to be validated
                      'value.length<1 || value.search(/[^a-zA-z0-9_]/)>=0', 
                                   // validation condition
                      '無效的用戶名:只能輸入6-20位字母、數(shù)字、下劃線的組合!'),
                                   // prompt on failure
                  new Array(
                      'password', 
                      'value.length<5 || value.search(/[^a-zA-z0-9_]/)>=0', 
                      '無效的密碼:只能輸入6-20位字母、數(shù)字、下劃線的組合!'),
                  new Array(
                      'email', 
                      'isMail(value)==false', 
                      'Email是您在網(wǎng)上的重要聯(lián)絡(luò)工具,請(qǐng)務(wù)必正確填寫!')
              );

              // Validate here, using eval statement.
              for(i = 0; i < elems.length; i++)
              {
                  with(eval('obj.'+elems[0]))
                  {
                      if(eval(elems[1]))
                      {
                          window.alert(elems[2]);
                          focus();
                          return false;
                      }
                  }
              }
              return true;
          }
          //-->
          </SCRIPT>
                  2、為form添加onsubmit屬性:
                     <form ... onsubmit='return frmValid(this)'>

           
          Javascript客戶端驗(yàn)證代碼 
           
          代碼: 

          --------------------------------------------------------------------------------
           
          <SCRIPT language=JavaScript><!-- 
          function passwordt(theForm){ 
              if (theForm.user_name.value == ""){ 
                  alert("登錄用戶名不能為空,請(qǐng)重新填寫!"); 
                  theForm.user_name.focus(); 
                  return (false); 
              } 

              if (theForm.user_pass.value == ""){ 
                  alert("登錄密碼不能為空,請(qǐng)重新填寫!"); 
                  theForm.user_pass.focus(); 
                  return (false); 
              } 
                               
              if (theForm.user_pass.value.length < 3){ 
                  alert("登錄密碼不能少于3個(gè)字符,請(qǐng)重新填寫!"); 
                  theForm.user_pass.focus(); 
                  return (false); 
              } 
                     
              if (theForm.re_password.value == ""){ 
                  alert("確認(rèn)新密碼不能為空,請(qǐng)重新填寫!"); 
                  theForm.re_password.focus(); 
                  return (false); 
              } 
                   
              if (theForm.re_password.value.length < 3){ 
                  alert("確認(rèn)新密碼不能少于3個(gè)字符,請(qǐng)重新填寫!"); 
                  theForm.re_password.focus(); 
                  return (false); 
              } 
                     
              if (theForm.user_pass.value != theForm.re_password.value){ 
                  alert("新密碼和確認(rèn)新密碼不同,請(qǐng)重新填寫!"); 
                  theForm.user_pass.focus(); 
                  return (false); 
              } 
                   
              if (theForm.user_namec.value=="") { 
                  alert ('請(qǐng)?zhí)顚懶彰?); 
                  theForm.user_namec.focus(); 
                  return false; 
              } 
               
              if ((!theForm.user_sex(0).checked)&&(!theForm.user_sex(1).checked)){ 
                  alert ('請(qǐng)選擇用戶性別!'); 
                  theForm.user_sex(0).focus(); 
                  return false; 
              } 

              if ((theForm.user_birth_year.value == "")||(fucCheckNUM(theForm.user_birth_year.value)==0)||(theForm.user_birth_year.value.length!=4)||((theForm.user_birth_year.value <1900) || (theForm.user_birth_year.value >2002 ) )) { 
                  alert("請(qǐng)?zhí)顚懻_的出生年份!"); 
                  theForm.user_birth_year.focus(); 
                  return (false); 
                 } 
                  
                 if (theForm.user_birth_month.value=="0"){ 
                  alert("請(qǐng)選擇出生月份!"); 
                  theForm.user_birth_month.focus(); 
                  return (false); 
                 } 
                  
                 if (theForm.user_birth_day.value=="0"){ 
                  alert("請(qǐng)選擇出生日期!"); 
                  theForm.user_birth_day.focus(); 
                  return (false); 
                 } 

                 if ((theForm.user_birth_month.value=="2") && (theForm.user_birth_day.value>29)){ 
                     alert("請(qǐng)選擇正確的出生日期!"); 
                  theForm.user_birth_month.focus(); 
                  return (false); 
              } 
                      
              if (((theForm.user_birth_month.value=="4")||(theForm.user_birth_month.value=="6")||(theForm.user_birth_month.value=="9")||(theForm.user_birth_month.value=="11")) && (theForm.user_birth_day.value==31)){ 
                  alert("請(qǐng)選擇正確的出生日期!"); 
                  theForm.user_birth_month.focus(); 
                  return (false); 
                 } 
               
              if (theForm.user_country.value=="") { 
                  alert("請(qǐng)選擇所在國(guó)家!"); 
                  theForm.user_country.focus(); 
                  return (false); 
              }     

              if (theForm.user_city.value =="") { 
                  alert("請(qǐng)選擇城市!"); 
                  theForm.user_city.focus(); 
                  return (false); 
              } 

              if (theForm.user_adds.value == "" ){ 
                  alert("家庭住址不能為空,請(qǐng)重新填寫!"); 
                  theForm.user_adds.focus(); 
                  return (false); 
              } 

              if (theForm.user_postcode.value == "" ) { 
                  alert("郵政編碼不能為空,請(qǐng)重新填寫!"); 
                  theForm.user_postcode.focus(); 
                  return (false); 
              }     

              if (theForm.user_tel.value == "" ) { 
                  alert("郵政編碼不能為空,請(qǐng)重新填寫!"); 
                  theForm.user_tel.focus(); 
                  return (false); 
              } 
               
              if (theForm.user_mail.value=="") { 
                  alert("請(qǐng)?zhí)顚慐MAIL!"); 
                  theForm.user_mail.focus(); 
                  return (false); 
              } 

              if ((theForm.user_mail.value!="") && (chkemail(theForm.user_mail.value)==0)) { 
                  alert("EMAIL格式不對(duì),請(qǐng)重新填寫!"); 
                  theForm.user_mail.focus(); 
                  return (false); 
              } 

              return (true); 


          function chkemail(a){ 
              var i=a.length; 
              var temp = a.indexOf('@'); 
              var tempd = a.indexOf('.'); 
              if (temp > 1) { 
                  if ((i-temp) > 3){ 
                      if ((i-tempd)>0){ 
                          return 1; 
                      } 
                  } 
              } 
              return 0; 


          function fucCheckNUM(NUM) { 
              var i,j,strTemp; 
              strTemp="0123456789"; 
              if ( NUM.length== 0) 
                  return 0 
              for (i=0;i<NUM.length;i++) { 
                  j=strTemp.indexOf(NUM.charAt(i)); 
                  if (j==-1) { 
                      return 0; 
                  } 
              } 
              return 1; 


          function chg_nation(nation){ 
              if (nation!='cn'){ 
                  ini_province(nation); 
                  document.user_form.selProvince.disabled = true; 
                  document.user_form.selProvince.value="國(guó)外城市"; 
                  document.user_form.selCity.value="國(guó)外城市"; 
              }else{ 
                  ini_province(nation); 
                  document.user_form.selProvince.disabled = false; 
                  document.user_form.selProvince.value="0"; 
                  document.user_form.selCity.value=""; 
              } 


          function chg_province(province){ 
              obj=document.getElementById("list_city"); 
              obj.src="list_city.php?province="+province; 


          //--> 
          </SCRIPT> 

          posts - 131, comments - 12, trackbacks - 0, articles - 32

          Copyright © yukui

          主站蜘蛛池模板: 孝昌县| 三江| 绥宁县| 南充市| 固阳县| 鸡泽县| 衢州市| 罗源县| 阿合奇县| 安泽县| 武冈市| 蕉岭县| 湛江市| 和田县| 旬阳县| 遵义市| 四川省| 章丘市| 浙江省| 容城县| 宜兰县| 阿图什市| 雷山县| 东乡县| 财经| 凤庆县| 松阳县| 同仁县| 寿宁县| 定边县| 大石桥市| 吉隆县| 开平市| 公主岭市| 大渡口区| 三原县| 收藏| 时尚| 托克逊县| 连江县| 沭阳县|