我们写Form时,如果每次都要写检验代码,那就太低效率了,这里有些常用的检验代码,与大家共享。
//----------------------------------------------------空格判断
function isWhitespace (s)
{
var whitespace = " /t/n/r";
var i;
for (i = 0; i < s.length; i++)
{
var c = s.charAt(i);
if (whitespace.indexOf(c) >= 0)
{
return true;
}
}
return false;
}
//--------------------------------------------------检验字符(限制不能输入的字符)
function ischar(s)
{
var errorChar;
var badChar = "><,[]{}?/+=|//'/":;~!@#$%^&()`";
if ( s == "" )
{
alert("请您输入内容!")
return false;
}
if ( isWhitespace(s) )
{
alert("输入的字符中不能包含空格符,请重新输入!");
return false;
}
errorChar = isCharsInBagEx( s, badChar)
if (errorChar != "" )
{
alert("您输入的字符" + s+"是无效的,/n/n请不要在字符中输入" + errorChar + "!/n/n请重新输入合法的字符!" );
return false;
}
return true;
}
//---------------------------------------------------Email的格式判断
function isEmail(s)
{
if (isEmpty(s))
{
alert("输入的E-mail地址不能为空,请输入!");
return false;
}
//is s contain whitespace
if (isWhitespace(s))
{
alert("输入的E-mail地址中不能包含空格符,请重新输入!");
return false;
}
var i = 1;
var len = s.length;
if (len > 40)
{
alert("E-mail地址长度不能超过40位!");
return false;
}
pos1 = s.indexOf("@");
pos2 = s.indexOf(".");
pos3 = s.lastIndexOf("@");
pos4 = s.lastIndexOf(".");
if ((pos1 <= 0)||(pos1 == len)||(pos2 <= 0)||(pos2 == len)){
alert("请输入有效的E-mail地址!");
return false;
}
else{
if( (pos1 == pos2 - 1) || (pos1 == pos2 + 1)
|| ( pos1 != pos3 ) //find two @
|| ( pos4 < pos3 ) ) //. should behind the '@'
{
alert("请输入有效的E-mail地址!");
return false;
}
}
if ( !isCharsInBag( s, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_@"))
{
alert("email地址中只能包含字符ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_@/n" + "请重新输入" );
return false;
}
return true;
}
//--------------------------------------------------数字判断
function isNum(s)
{
if (isEmpty(s)){
alert("必须输入数字")
return false;
}
if(!isCharsInBag (s, "0123456789")){
alert("请检查一下您输入的是否为数字!");
return false;
}
// if (s<1){
// alert("输入的数字要大于0!");
// return false;
// }
// if (s>2000){
// alert("输入的数字要小于2000!");
// return false;
// }
return true;
}
//--------------------------------------------------身份证判断
function isCardNum(s)
{
if (isEmpty(s)){
alert("必须输入数字")
return false;
}
if(!isCharsInBag (s, "0123456789")){
alert("请检查一下您输入的是否为数字?quot;);
return false;
}
if (s.length==15 || s.length==18){
return true;
}else{
alert("输入的数字长度为15位或者18位!");
return false;
}
}
//--------------------------------------------------身份证判断(与出生年月日对照)
function isIdCardNo(num)
{
if (isNaN(num)) {alert("输入的不是数字!"); return false;}
var len = num.length, re;
if (len == 15)
re = new RegExp(/^(/d{6})()?(/d{2})(/d{2})(/d{2})(/d{3})$/);
else if (len == 18)
re = new RegExp(/^(/d{6})()?(/d{4})(/d{2})(/d{2})(/d{3})(/d)$/);
else {alert("输入的数字位数不对!"); return false;}
var a = num.match(re);
if (a != null)
{
if (len==15)
{
var D = new Date("19"+a[3]+"/"+a[4]+"/"+a[5]);
var B = D.getYear()==a[3]&&(D.getMonth()+1)==a[4]&&D.getDate()==a[5];
}
else
{
var D = new Date(a[3]+"/"+a[4]+"/"+a[5]);
var B = D.getFullYear()==a[3]&&(D.getMonth()+1)==a[4]&&D.getDate()==a[5];
}
if (!B) {alert("输入的身份证号 "+ a[0] +" 里出生日期不对!"); return false;}
}
return true;
}
//--------------------------------------------------身份证严格验证
<script>
var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"}
function cidInfo(sId){
var iSum=0
var info=""
if(!/^/d{17}(/d|x)$/i.test(sId))return false;
sId=sId.replace(/x$/i,"a");
if(aCity[parseInt(sId.substr(0,2))]==null)return "Error:非法地区";
sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));
var d=new Date(sBirthday.replace(/-/g,"/"))
if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))return "Error:非法生日";
for(var i = 17;i>=0;i --) iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11)
if(iSum%11!=1)return "Error:非法证号";
return aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女")
}
document.write(cidInfo("380524198002300016"),"<br/>");
document.write(cidInfo("340524198002300019"),"<br/>")
document.write(cidInfo("340524197711111111"),"<br/>")
document.write(cidInfo("34052419800101001x"),"<br/>");
</script>
//--------------------------------------------------邮政编码判断
function isZipCode(s){
if (!isEmpty(s)){
if(!isCharsInBag (s, "0123456789")){
alert("请检查一下您输入的是否为数字!");
return false;
}
if (s.length==6){
return true;
}else{
alert("输入的邮政编码长度为6!");
return false;
}
} else {
return true;
}
}
//---------------------------------------------------检验字符是否在S中
function isCharsInBag (s, bag)
{
var i;
// Search through string's characters one by one.
// If character is in bag, append to returnString.
for (i = 0; i < s.length; i++)
{
// Check that current character isn't whitespace.
var c = s.charAt(i);
if (bag.indexOf(c) == -1) return false;
}
return true;
}
function isEmpty(s)
{
return ((s == null)||(s.length == 0));
//----------------------------------------------------年份判断
function isYear(s)
{
var Today=new Date()
thisYear=Today.getYear()
var badChar = "><,[]{}?/+=|//'/":;~!@#$%^&`";
var len = s.length;
if (isEmpty(s)){
alert("必须输入年份")
return false;
}
if ( isCharsInBag( s, badChar)){
alert("年份输入非法")
return false;
}
if ((len!=4)){
alert("年份是四位!");
return false;
}
if(!isCharsInBag (s, "0123456789")){
alert("请检查一下您输入的年份是否为数字!");
return false;
}
if (s<1800){
alert("年份要大于1800");
return false;
}
// if ((thisYear-s)<0){
// alert("年份要不能大于"+thisYear);
// return false;
// }
return true;
}
}
//----------------------------------------------------判断是否选择单选按钮
function radioselected(radioBth)
{
for(i=0;i<radioBth.length;i++)
{
if(radioBth[i].checked)
return true
}
return false
}
//----------------------------------------------------检查是否为电话号码
function fucCheckTEL(TEL)
{
var i,j,strTemp;
strTemp="0123456789-()# ";
for (i=0;i<TEL.length;i++)
{
j=strTemp.indexOf(TEL.charAt(i));
if (j==-1)
{
return false;
}
}
return true;
}
//----------------------------------------------------严格电话号码的验证
要求:
(1)电话号码由数字、"("、")"和"-"构成
(2)电话号码为3到8位
(3)如果电话号码中包含有区号,那么区号为三位或四位
(4)区号用"("、")"或"-"和其他部分隔开
(5)移动电话号码为11或12位,如果为12位,那么第一位为0
(6)11位移动电话号码的第一位和第二位为"13"
(7)12位移动电话号码的第二位和第三位为"13"
根据这几条规则,可以与出以下正则表达式:
(^[0-9]{3,4}/-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^/([0-9]{3,4}/)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)
<script language="javascript">
function PhoneCheck(s) {
var str=s;
var reg=/(^[0-9]{3,4}/-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^/([0-9]{3,4}/)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/
alert(reg.test(str));
}
</script>
<input type=text name="iphone">
<input type=button οnclick="PhoneCheck(document.all.iphone.value)" value="Check">
//具有在输入非数字字符不回显的效果,即对非数字字符的输入不作反应。
function numbersonly(field,event){
var key,keychar;
if(window.event){
key = window.event.keyCode;
}
else if (event){
key = event.which;
}
else{
return true
}
keychar = String.fromCharCode(key);
if((key == null)||(key == 0)||(key == 8)||(key == 9)||(key == 13)||(key == 27)){
return true;
}
else if(("0123456789.").indexOf(keychar)>-1){
window.status = "";
return true;
}
else {
window.status = "Field excepts numbers only";
return false;
}
}