如何验证15位身份证号码

请问如何验证15位身份证号码?-在线等待

在线等待


大家帮帮忙?gt;>?br>

ValidationExpression="^\d{15}$"


最好能给我完整的代码,我不需要政则表达式。谢谢。


楼上说得正确
ValidationExpression="^\d{15}$"


<asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server"
              ControlToValidate="TextBox1"
              ValidationExpression="^\d{15}$"
              Display="Static"
              Font-Name="verdana" 
              Font-Size="10pt">
                 请输入正确的身份号码!
          </asp:RegularExpressionValidator>


我不要这个脚本的语言,我要的是一个function的函数,验证通过,返回true,验证错误,返回flase。


你想怎么验证?gt;>9D阌置挥泄不氐纳矸葜た猓阕疃嘌橹こざ榷圆欢裕蚴前焉矸葜さ哪暝氯仗岢隼矗胗没ё砸烟畹哪暝氯展ぷ识员龋词欠裣嗤渌鼓苎橹な裁矗?br>

郁闷,得不到我想要得答案,如何结贴?


以前写的,根据你的具体环境做一些调整吧
         //校验出生日期和18位的校验和
         //局限:日期校验的时候是1900-1999,可以根据具体的需要调整
         //Test Passed in IE6.0
         //By pxk, pxknet@163.com 
//检查身份证号,支持15和18位。返回值表示错误原因。""表示无错
function CheckIDCard(strID)
{
//验证各位字符是否合法的正则表达式
ReDigital15 = /\d{15}/;
ReDigital18 = /\d{17}[0-9xX]{1}/;
//提取日期和校验的正则表达式
ReDate15 = /\d{6}(\d{6})\d{3}/;
ReDate18 = /\d{6}(\d{8})\d{3}/;
switch(strID.length)
{
case 15:
if( ReDigital15.test( strID ) == false )
return "非法字符";
Arr = ReDate15.exec( strID );
strDate = "19" + Arr[1];
if( CheckDate(strDate, new Date(1900,0,1), new Date(1999,11,31)) == false )
return "日期无效";
break;
case 18:
if( ReDigital18.test( strID ) == false )
return "非法字符";
Arr = ReDate18.exec( strID );
strDate = Arr[1];
if( CheckDate(strDate, new Date(1900,0,1), new Date()) == false )
return "日期无效";
if( CheckSum( strID ) == false )
return "校验错误";
break;
default:
return "位数不对";
break;
}
return "";
}
//对身份证的校验位进行验证
function CheckSum( strID )
{
//debugger;
//18位数字提取正则表达式
Re18Digital = /(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})([0-9xX]{1})/;
Arr = Re18Digital.exec(strID);
var Wi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
Sum = 0;
for(i=0;i<=16;i++)
Sum += Arr[i+1] * Wi[i];
ArrCheckSum = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
strCheckSum = ArrCheckSum[Sum%11];
if( strCheckSum == Arr[18].toUpperCase() )
return true;
else
return false;
}
//检查在指定范围之内的日期,支持选择框自动生成格式和手动连打格式。返回bool,true表示日期有效,false表示日期无效。
function CheckDate( strDate, DateFrom, DateTo )
{
//手动日期验证正则表达式
ReDigital8 = /\d{8}/;
//自动日期验证正则表达式
ReAutoDate = /\d{4}-{1}\d{1,2}-\d{1,2}/;
//自动日期提取正则表达式
//ReGetDate = /(\d{4})-{1}(\d{1,2})-(\d{1,2})/;
//debugger;
if(strDate.indexOf("-")>-1)
{
if( ReAutoDate.test( strDate ) == false )
return false;
//Arr = ReGetDate.exec( strDate );
Arr = strDate.split("-");
strDate = (Arr[0]) + "" + (Arr[1].length<2?"0":"") + Arr[1] + (Arr[2].length<2?"0":"") + (Arr[2]);
}
if(strDate.length!=8)
return false;
if( ReDigital8.test(strDate)==false )
return false;
MyDate = eval( strDate.replace( /^(\d{4})(\d{2})(\d{2})$/, "new Date($1,$2-1,$3)" ) );
strMyDate = MyDate.getFullYear()+(MyDate.getMonth()<9?"0":"")+(MyDate.getMonth()+1)+""+(MyDate.getDate()<=9?"0":"")+MyDate.getDate();
//日期有效性验证
if(strMyDate!=strDate)
return false;
//日期范围验证
if( MyDate>=DateFrom && MyDate<=DateTo )
return true;
else
return false;
}


ValidationExpression="^\d{15}$"


对啊,你如果不能连到公安机关数据库去查询的话,这样做无意义


楼上说的对,确实!


Public Function getCheckCode(ByVal SFZH As String) As String
        Dim strJiaoYan() As Char = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"}
        Dim intQuan() As Integer = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1}
        Dim strTemp As String
        Dim intTemp As String
        Dim i As Integer
        strTemp = SFZH.Substring(0, 6) & "19" & SFZH.Substring(6)
        For i = 0 To strTemp.Length - 1
            intTemp = intTemp + Convert.ToInt32(strTemp.Substring(i, 1)) * intQuan(i)
        Next
        intTemp = intTemp Mod 11
        Return strTemp & strJiaoYan(intTemp)
    End Function
http://community.csdn.net/Expert/topic/3891/3891579.xml?temp=.5628015


如果用的是VS的话
就在aspx页子里拉除一个ValidationExpression验证控件
然后在属性里定义^\d{15}$


身份证号并不都是十五位,应搞个取值范围吧>>如15-18


有个算法的。


15位身份证验证:
位数:15位
内容全部为数值
7-12位 为出生日期
就这么几项,写个函数很简单呀


如果不能连到公安机关数据库去查询的话,就只有判断一下是不是15位数字,其他的都没有意思.用正则表达式就可以了


就像楼上说的,要么15就18位,不能限死330的,下面是正则表达式(我不喜欢自带的验证控件,从Label继承的,占地方!)
public string IsID(string Inputvalue)
{   /* 判断输入是否为身份证*/
if(Inputvalue=="")return "";
string pattern = @"([0-9]{14}[x0-9]{1})|([0-9]{17}[x0-9]{1})";
Match m = Regex.Match(Inputvalue,pattern);
if(m.Success)
{
return "";
}
else
{
return "身份证格式不正确";
}
}


\d{18}|\d{15}


18位的话,最后一位有可能是 字母


VS里的RegularExpressionValidator控件,设置ValidationExpression为中国身份证验证
但是这样的验证不是很精确只是接受15位或18位的数字,更确切的验证要在服务器端进行身份证规则判断


再Up


我也要这样的验证方法啊,一直无犁头


这是我目前使用的验证17位身份证号码的函数:
Public Shared Function CheckCidInfo(ByVal cid As String) As Boolean
        Dim aCity() As String = {"", "", "", "", "", "", "", "", "", "", _
                                "", "北京", "天津", "河北", "山西", "内蒙古", "", _
                                "", "", "", "", "辽宁", "吉林", "黑龙江", "", "", _
                                "", "", "", "", "", "上海", "江苏", "浙江", "安微", _
                                "福建", "江西", "山东", "", "", "", "河南", "湖北", _
                                "湖南", "广东", "广西", "海南", "", "", "", "重庆", _
                                "四川", "贵州", "云南", "西藏", "", "", "", "", "", _
                                "", "陕西", "甘肃", "青海", "宁夏", "新疆", "", "", _
                                "", "", "", "台湾", "", "", "", "", "", "", "", "", _
                                "", "香港", "澳门", "", "", "", "", "", "", "", "", "国外"}
        Dim iSum As Double = 0
        Dim info As String = ""
        Dim rg As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("^\d{17}(\d|x)$")
        Dim mc As System.Text.RegularExpressions.Match = rg.Match(cid)
        If mc.Success = False Then
            Return False
        End If
        cid = cid.ToLower
        cid = cid.Replace("x", "a")
        If aCity(Integer.Parse(cid.Substring(0, 2))) = "" Then
            'Return "非法地址"
            Return False
        End If
        Try
            DateTime.Parse(cid.Substring(6, 4) & "-" & cid.Substring(10, 2) & "-" & cid.Substring(12, 2))
        Catch ex As Exception
            'Return "非法生日"
            Return False
        End Try
        Dim i As Integer
        For i = 17 To 0 Step -1
            iSum = iSum + (System.Math.Pow(2, i) Mod 11) * Integer.Parse(cid.Substring(17 - i, 1), System.Globalization.NumberStyles.HexNumber)
            '
        Next
        If iSum Mod 11 <> 1 Then
            'Return "非法证号"
            Return False
        End If
        Dim strSex As String
        If (Integer.Parse(cid.Substring(16, 1)) Mod 2) = 1 Then
            strSex = "男"
        Else
            strSex = "女"
        End If
        'Return aCity(Integer.Parse(cid.Substring(0, 2))) & "," & cid.Substring(6, 4) & "-" & cid.Substring(10, 2) & "-" & cid.Substring(12, 2) & "," & strSex
        Return True
End Function
本人只想要一个和这个差不多的验证函数。


如果不能连到公安机关数据库去查询的话,就只有判断一下是不是15或18位数字,其他的根据特定的要求.用正则表达式就可以了


好的,结贴。
posted @ 2006-01-21 18:37  致远钓客  阅读(10625)  评论(5)    收藏  举报