身份证限制正确输入的脚本
如题,jsp页面输入框标签s:textfield,想触发一个onkeydown事件。用脚本限制只能输入正确的身份证格式。望高手赐教O(∩_∩)O~(废话者and装懂蒙事者俺举报)
补充:说的就是JAVASCRIPT脚本- -!
如题,jsp页面输入框标签s:textfield,想触发一个onkeydown事件。用脚本限制只能输入正确的身份证格式。望高手赐教O(∩_∩)O~(废话者and装懂蒙事者俺举报)
补充:说的就是JAVASCRIPT脚本- -!
答案:可以用简单的正则表达式验证
document.getElementById("textfield").value.match(/d{17}[/dxX]|/d{15})
这样只能验证用户输入的是18位或15的数字 包括有的身份证最后两位是XX
如果要详细验证 下面是验证代码
private string CheckCidInfo(string cid)
{
string[] aCity = new string[]{null,null,null,null,null,null,null,null,null,null,null,"北京","天津","河北","山西","内蒙古",null,null,null,null,null,"辽宁","吉林","黑龙江",null,null,null,null,null,null,null,"上海","江苏","浙江","安微","福建","江西","山东",null,null,null,"河南","湖北","湖南","广东","广西","海南",null,null,null,"重庆","四川","贵州","云南","易做图",null,null,null,null,null,null,"陕西","甘肃","青海","宁夏","新疆",null,null,null,null,null,"台湾",null,null,null,null,null,null,null,null,null,"香港","澳门",null,null,null,null,null,null,null,null,"国外"};
double iSum=0;
string info="";
System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{17}(\d|x)$");
System.Text.RegularExpressions.Match mc = rg.Match(cid);
if(!mc.Success)
{
return "";
}
cid = cid.ToLower();
cid = cid.Replace("x","a");
if(aCity[int.Parse(cid.Substring(0,2))]==null)
{
return "非法地区";
}
try
{
DateTime.Parse(cid.Substring(6,4)+"-"+cid.Substring(10,2)+"-"+cid.Substring(12,2));
}
catch
{
return "非法生日";
}
for(int i=17;i>=0;i--)
{
iSum +=(System.Math.Pow(2,i)%11)*int.Parse(cid[17-i].ToString(),System.Globalization.NumberStyles.HexNumber);
}
if(iSum%11!=1)
return("非法证号");
return(aCity[int.Parse(cid.Substring(0,2))]+","+cid.Substring(6,4)+"-"+cid.Substring(10,2)+"-"+cid.Substring(12,2)+","+(int.Parse(cid.Substring(16,1))%2==1?"男":"女"));
}
调用测试:
Response.Write(CheckCidInfo("340524198002300019"));
Response.Write(CheckCidInfo("34052419800101001x"));
一般的网站还是用第一种好用JAVASCRIPT脚本就可以写出来
上一个:关于flash全屏脚本问题
下一个:应该选择哪种脚本语言来进行入门学习?