js驗證表單

來源:互聯網
上載者:User

標籤:china   reg   不能   port   [1]   規則   checkbox   parameter   cte   

/*用途:校正ip地址的格式 輸入:strIP:ip地址 返回:假設通過驗證返回true,否則返回false。 */ function isIP(strIP) { if (isNull(strIP)) return false; var re=/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/g //匹配IP地址的正則表達式 if(re.test(strIP)) { if( RegExp.$1 <256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256) return true; } return false; } /* 用途:檢查輸入字串是否為空白或者所有都是空格 輸入:str 返回: 假設全是空返回true,否則返回false */ function isNull( str ){ if ( str == "" ) return true; var regu = "^[ ]+$"; var re = new RegExp(regu); return re.test(str); }   /* 用途:檢查輸入對象的值是否符合整數格式 輸入:str 輸入的字串 返回:假設通過驗證返回true,否則返回false */ function isInteger( str ){  var regu = /^[-]{0,1}[0-9]{1,}$/; return regu.test(str); } /* 用途:檢查輸入手機號碼是否正確 輸入: s:字串 返回: 假設通過驗證返回true,否則返回false */ function checkMobile( s ){   var regu =/^[1][3][0-9]{9}$/; var re = new RegExp(regu); if (re.test(s)) { return true; }else{ return false; } }   /* 用途:檢查輸入字串是否符合正整數格式 輸入: s:字串 返回: 假設通過驗證返回true,否則返回false */ function isNumber( s ){   var regu = "^[0-9]+$"; var re = new RegExp(regu); if (s.search(re) != -1) { return true; } else { return false; } } /* 用途:檢查輸入字串是否是帶小數的數字格式,能夠是負數 輸入: s:字串 返回: 假設通過驗證返回true,否則返回false */ function isDecimal( str ){   if(isInteger(str)) return true; var re = /^[-]{0,1}(\d+)[\.]+(\d+)$/; if (re.test(str)) { if(RegExp.$1==0&&RegExp.$2==0) return false; return true; } else { return false; } } /* 用途:檢查輸入對象的值是否符合port號格式 輸入:str 輸入的字串 返回:假設通過驗證返回true,否則返回false */ function isPort( str ){  return (isNumber(str) && str<65536); } /* 用途:檢查輸入對象的值是否符合E-Mail格式 輸入:str 輸入的字串 返回:假設通過驗證返回true,否則返回false */ function isEmail( str ){  var myReg = /^[-_A-Za-z0-9][email protected]([_A-Za-z0-9]+\.)+[A-Za-z0-9]{2,3}$/; if(myReg.test(str)) return true; return false; } /* 用途:檢查輸入字串是否符合金額格式 格式定義為帶小數的正數,小數點後最多三位 輸入: s:字串 返回: 假設通過驗證返回true,否則返回false */ function isMoney( s ){   var regu = "^[0-9]+[\.][0-9]{0,3}$"; var re = new RegExp(regu); if (re.test(s)) { return true; } else { return false; } } /* 用途:檢查輸入字串是否僅僅由英文字母和數字和底線組成 輸入: s:字串 返回: 假設通過驗證返回true,否則返回false */ function isNumberOr_Letter( s ){//推斷是否是數字或字母 var regu = "^[0-9a-zA-Z\_]+$"; var re = new RegExp(regu); if (re.test(s)) { return true; }else{ return false; } } /* 用途:檢查輸入字串是否僅僅由英文字母和數字組成 輸入: s:字串 返回: 假設通過驗證返回true,否則返回false */ function isNumberOrLetter( s ){//推斷是否是數字或字母 var regu = "^[0-9a-zA-Z]+$"; var re = new RegExp(regu); if (re.test(s)) { return true; }else{ return false; } } /* 用途:檢查輸入字串是否僅僅由漢字、字母、數字組成 輸入: value:字串 返回: 假設通過驗證返回true,否則返回false */ function isChinaOrNumbOrLett( s ){//推斷是否是漢字、字母、數字組成 var regu = "^[0-9a-zA-Z\u4e00-\u9fa5]+$";   var re = new RegExp(regu); if (re.test(s)) { return true; }else{ return false; } } /* 用途:推斷是否是日期 輸入:date:日期;fmt:日期格式 返回:假設通過驗證返回true,否則返回false */ function isDate( date, fmt ) { if (fmt==null) fmt="yyyyMMdd"; var yIndex = fmt.indexOf("yyyy"); if(yIndex==-1) return false; var year = date.substring(yIndex,yIndex+4); var mIndex = fmt.indexOf("MM"); if(mIndex==-1) return false; var month = date.substring(mIndex,mIndex+2); var dIndex = fmt.indexOf("dd"); if(dIndex==-1) return false; var day = date.substring(dIndex,dIndex+2); if(!isNumber(year)||year>"2100" || year< "1900") return false; if(!isNumber(month)||month>"12" || month< "01") return false; if(day>getMaxDay(year,month) || day< "01") return false; return true; } function getMaxDay(year,month) { if(month==4||month==6||month==9||month==11) return "30"; if(month==2) if(year%4==0&&year%100!=0 || year%400==0) return "29"; else return "28"; return "31"; } /* 用途:字元1是否以字串2結束 輸入:str1:字串;str2:被包括的字串 返回:假設通過驗證返回true,否則返回false */ function isLastMatch(str1,str2) {  var index = str1.lastIndexOf(str2); if(str1.length==index+str2.length) return true; return false; }   /* 用途:字元1是否以字串2開始 輸入:str1:字串;str2:被包括的字串 返回:假設通過驗證返回true,否則返回false */ function isFirstMatch(str1,str2) {  var index = str1.indexOf(str2); if(index==0) return true; return false; } /* 用途:字元1是包括字串2 輸入:str1:字串;str2:被包括的字串 返回:假設通過驗證返回true,否則返回false */ function isMatch(str1,str2) {  var index = str1.indexOf(str2); if(index==-1) return false; return true; }   /* 用途:檢查輸入的起止日期是否正確。規則為兩個日期的格式正確, 且結束如期>=起始日期 輸入: startDate:起始日期。字串 endDate:結束如期。字串 返回: 假設通過驗證返回true,否則返回false */ function checkTwoDate( startDate,endDate ) { if( !isDate(startDate) ) { alert("起始日期不對!"); return false; } else if( !isDate(endDate) ) { alert("終止日期不對!"); return false; } else if( startDate > endDate ) { alert("起始日期不能大於終止日期!"); return false; } return true; } /* 用途:檢查輸入的Email信箱格式是否正確 輸入: strEmail:字串 返回: 假設通過驗證返回true,否則返回false */ function checkEmail(strEmail) { //var emailReg = /^[_a-z0-9][email protected]([_a-z0-9]+\.)+[a-z0-9]{2,3}$/; var emailReg = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/; if( emailReg.test(strEmail) ){ return true; }else{ alert("您輸入的Email地址格式不對!"); return false; } } /* 用途:檢查輸入的電話號碼格式是否正確 輸入: strPhone:字串 返回: 假設通過驗證返回true,否則返回false */ function checkPhone( strPhone ) { var phoneRegWithArea = /^[0][1-9]{2,3}-[0-9]{5,10}$/; var phoneRegNoArea = /^[1-9]{1}[0-9]{5,8}$/; var prompt = "您輸入的電話號碼不對!" if( strPhone.length > 9 ) { if( phoneRegWithArea.test(strPhone) ){ return true; }else{ alert( prompt ); return false; } }else{ if( phoneRegNoArea.test( strPhone ) ){ return true; }else{ alert( prompt ); return false; }   } }   /* 用途:檢查複選框被選中的數目 輸入: checkboxID:字串 返回: 返回該複選框中被選中的數目 */ function checkSelect( checkboxID ) { var check = 0; var i=0; if( document.all(checkboxID).length > 0 ) { for(  i=0; i<document.all(checkboxID).length; i++ ) { if( document.all(checkboxID).item( i ).checked  ) { check += 1; }     } }else{ if( document.all(checkboxID).checked ) check = 1; } return check; } function getTotalBytes(varField) { if(varField == null) return -1; var totalCount = 0; for (i = 0; i< varField.value.length; i++) { if (varField.value.charCodeAt(i) > 127) totalCount += 2; else totalCount++ ; } return totalCount; } function getFirstSelectedValue( checkboxID ){ var value = null; var i=0; if( document.all(checkboxID).length > 0 ){ for(  i=0; i<document.all(checkboxID).length; i++ ){ if( document.all(checkboxID).item( i ).checked ){ value = document.all(checkboxID).item(i).value; break; } } } else { if( document.all(checkboxID).checked ) value = document.all(checkboxID).value; } return value; }   function getFirstSelectedIndex( checkboxID ){ var value = -2; var i=0; if( document.all(checkboxID).length > 0 ){ for(  i=0; i<document.all(checkboxID).length; i++ ) { if( document.all(checkboxID).item( i ).checked  ) { value = i; break; } } } else { if( document.all(checkboxID).checked ) value = -1; } return value; } function selectAll( checkboxID,status ){ if( document.all(checkboxID) == null) return; if( document.all(checkboxID).length > 0 ){ for(  i=0; i<document.all(checkboxID).length; i++ ){ document.all(checkboxID).item( i ).checked = status; } } else { document.all(checkboxID).checked = status; } } function selectInverse( checkboxID ) { if( document.all(checkboxID) == null) return; if( document.all(checkboxID).length > 0 ) { for(  i=0; i<document.all(checkboxID).length; i++ ) { document.all(checkboxID).item( i ).checked = !document.all(checkboxID).item( i ).checked; } } else { document.all(checkboxID).checked = !document.all(checkboxID).checked; } } function checkDate( value ) { if(value==‘‘) return true; if(value.length!=8 || !isNumber(value)) return false;  var year = value.substring(0,4); if(year>"2100" || year< "1900") return false; var month = value.substring(4,6); if(month>"12" || month< "01") return false; var day = value.substring(6,8); if(day>getMaxDay(year,month) || day< "01") return false; return true;  } /* 用途:檢查輸入的起止日期是否正確,規則為兩個日期的格式正確或都為空白 且結束日期>=起始日期 輸入: startDate:起始日期。字串 endDate:  結束日期。字串 返回: 假設通過驗證返回true,否則返回false */ function checkPeriod( startDate,endDate ) { if( !checkDate(startDate) ) { alert("起始日期不對!"); return false; } else if( !checkDate(endDate) ) { alert("終止日期不對!"); return false; } else if( startDate > endDate ) { alert("起始日期不能大於終止日期!"); return false; } return true; } /* 用途:檢查證券代碼是否正確 輸入: secCode:證券代碼 返回: 假設通過驗證返回true,否則返回false */ function checkSecCode( secCode ) { if( secCode.length !=6 ){ alert("證券代碼長度應該為6位"); return false; } if(!isNumber( secCode ) ){ alert("證券代碼僅僅能包括數字");   return false; } return true; } /**************************************************** function:cTrim(sInputString,iType) description:字串去空格的函數 parameters:iType:1=去掉字串左邊的空格 2=去掉字串左邊的空格 0=去掉字串左邊和右邊的空格 return value:去掉空格的字串 ****************************************************/ function cTrim(sInputString,iType) { var sTmpStr = ‘ ‘; var i = -1; if(iType == 0 || iType == 1) { while(sTmpStr == ‘ ‘) { ++i; sTmpStr = sInputString.substr(i,1); } sInputString = sInputString.substring(i); } if(iType == 0 || iType == 2) { sTmpStr = ‘ ‘; i = sInputString.length; while(sTmpStr == ‘ ‘) { --i; sTmpStr = sInputString.substr(i,1); } sInputString = sInputString.substring(0,i+1); } return sInputString; } 


js驗證表單

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.