ID号和单元号验证 [英] ID number and Cell Number validation

查看:65
本文介绍了ID号和单元号验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何验证asp.net中的ID号和单元号?我正在尝试将用户注册到系统,所以我需要检查ID号是否有效。例如,我们在南非有一个13位数的身份证号码(意思是第一年,第二个月,你出生的那天[意思是750910-意味着你出生于1975年9月的第十天])和10位数的一个细胞数。

How to validate ID number and cell number in asp.net? i am trying to register users to the system, so i need to check if the ID number is valid. for instance we have 13 digits for id number in South Africa (meaning year first, month second, day in which u were born[meaning 750910- means you were born in 1975 september on the tenth day]) and ten digits for a cell number.

推荐答案

这就是我解决它的方法



this is how i solved it

//call validation method
if (validate_SA_ID(txtIdNumber.Text) == true)
{
//taking user iput code

}
else if (validate_SA_ID(txtIdNumber.Text) == false)
{
//error msg

}

// validation method
 private bool validate_SA_ID(string id_num)
        {

            if (id_num == "0000000000000")
            {
                return false;
            }
            bool IsValid = false;
            if (id_num.Length == 13)
            {
                int c, odds, result1, result2, result3, dsada;
                odds = 0;
                result2 = 0;
                string evens = "";
                for (c = 0; c < 12; c++)
                {
                    if (c % 2 == 0)
                        odds += Convert.ToInt32(id_num.Substring(c, 1));
                    else
                        evens += id_num.Substring(c, 1);
                }
                result1 = Convert.ToInt32(evens) * 2;
                string res = result1.ToString();
                int k;
                for (k = 0; k < res.Length; k++)
                {
                    result2 += Convert.ToInt32(res.Substring(k, 1));
                }
                result3 = odds + result2;
                dsada = 10 - result3 % 10;
                if (dsada.ToString().Length == 1)
                {
                    if (Convert.ToInt32(id_num.Substring(12)) == dsada)
                    {
                        IsValid = true;
                    }
                    else
                        IsValid = false;

                }
                else
                {
                    if (dsada.ToString().Substring(1, 1) == id_num.Substring(12, 1))
                    {
                        IsValid = true;
                    }
                    else
                    {
                        IsValid = false;

                    }
                }
            }
            return IsValid;

        }


没有人强迫你使用正则表达式和其他。首先使用Regex确保所有字符都是数字(或根据需要应用其他一些条件),然后使用 string.Length



-SA
Nobody force you to use Regex and nuthing else. First use Regex to make sure all characters are numbers (or apply some other criteria, as required), and then validate that the length of the string is the same as expected, using string.Length.

—SA


这篇关于ID号和单元号验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆