js验证代码

1.JS判断是否是邮编

<script>
function isPostalCode(s)
{
//var patrn=/^[a-zA-Z0-9]{3,12}$/;
var patrn=/^[a-zA-Z0-9 ]{3,12}$/;
if (!patrn.exec(s)) return false
return true
}
var a = "315400";
alert(a+"是否合法!");
alert(isPostalCode(a));
a = "ab";
alert(a+"是否合法!");
alert(isPostalCode(a));
</script>

2.JS判断电话传真号码是否有效

<script>
function isTel(s)
{
var patrn=/^[+]{0,1}(d){1,3}[ ]?([-]?((d)|[ ]){1,12})+$/;
if (!patrn.exec(s)) return false
return true
}
var a = "0574-56158888";
alert(a+"是否合法!");
alert(isTel(a));
a = "123456a";
alert(a+"是否合法!");
alert(isTel(a));
</script>

3.js正则判断:只能输入5-20个以字母开头、可带数字、“_”、“.”的字符串

<script>
function isRegisterUserName(s)
{
var patrn=/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/;
if (!patrn.exec(s)) return false
return true
}
var a = "abc";
alert(a+"是否合法!");
alert(isRegisterUserName(a));
a = "abcdef";
alert(a+"是否合法!");
alert(isRegisterUserName(a));
</script>

4.判断身份证号码的JS函数

<script type="text/javascript">
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 false;
}
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 false;
}
for(var i = 17;i>=0;i --)
{
iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11)
}
if(iSum%11!=1)
{
return false;
}
return true;
}
alert(cidInfo("3432423432423432"));
</script>

5.验证日期是否合法的JS函数

<script type="text/javascript">
function isDateFormat(txt){ //是否为合法的日期格式:YYYY-MM-DD
if(txt==null || txt == ""){return false;}
else{
var regex = /[0-9]{1,4}-[0-9]{1,2}-[0-9]{1,2}/;
if( regex.test(txt) ){
var noArr = txt.split("-");
var year = eval(noArr[0]);
var month = eval(noArr[1]);
var day = eval(noArr[2]);
if ( year < 1 || month < 1 || month > 12 || day < 1 || day > 31) {return false;}
if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30){return false;}
if (month == 2){
if ((year % 4 != 0) && day > 29) { return false;}
if (year % 4 == 0){
if(year % 100 == 0 && year % 400 != 0 && day > 29){return false;}
else if (day > 28){ return false;}
}
}
return true;
}else{return false;}
}
}
alert("2008-05-11是日期吗?答:"+isDateFormat("2008-05-11"));
</script>

6.验证手机号码固定电话号码的JS函数

<script type="text/javascript">
function isPhoneNum(txt){ //检测是否为电话号码(固定电话或手机)
return isPhone(txt)||isMobile(txt);
}
function isPhone(txt){//检测是否为固定电话号码
if(txt == null || txt == ""){return false;}
else{
var regex = /[0-9]{1}[0-9]{2,3}-[1-9]{1}[0-9]{5,8}/;
return regex.test(txt);
}
}
function isMobile(txt){//检测是否为手机号码
if(txt == null || txt == ""){return false;}
else{
var regex = /13[0-9]{9}/;
return regex.test(txt);
}
}
alert(isPhone("0574-56157573"));
alert(isMobile("0574-56157574"));
alert(isMobile("13956122541"));
</script>

7.控制文本框输入长度的例子

<script>
function test()
{
if(a.value.length>10){
alert("不能超过10个字符");
a.focus();
}
else
top.location.href="http://www.baidu.com";
}
</script>
<input type="text" name=a><input type="button" value="测试" onclick="test();">

8.JS判断中文的正则表达式

<script>
function strChinese(str){
var pattern = /[^u4E00-u9FA5]/;
return !pattern.test(str);
}
alert(strChinese("中文"))
alert(strChinese("abc"))
alert(strChinese("中文Abc"))
</script>

9.验证email邮箱地址的js

<script type="text/javascript">
function isEmail(str){
res = /^[0-9a-zA-Z_-.]+@[0-9a-zA-Z_-]+(.[0-9a-zA-Z_-]+)*$/;
var re = new RegExp(res);
return !(str.match(re) == null);
}
alert(isEmail("abc.com"));
alert(isEmail(""));
</script>

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注