防止文本框中的电话号码 [英] prevent a phone number in text box

查看:51
本文介绍了防止文本框中的电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想阻止用户使用他们的电话号码作为用户名。所以我用后面的代码进行验证。如果用户名验证为7到10个数字,则会显示错误消息。在某种程度上,我以相反的顺序使用验证,即如果验证,则取消该过程并且仅在例外的情况下进行程序。我的代码是

I wanted to prevent users from using their phone number as username. So I used code behind for validation. and if the username is validated as between 7 to 10 numbers, it gives an error message. In a way I have used validation in reverse order i.e. if validated, the process is cancelled and program proceeds only in case of exception. My code for this is

protected void RegisterUser_CreatingUser(object sender, LoginCancelEventArgs e)
    {
            try
            {
                myreg1.Validate(RegisterUser.UserName);
                InvalidUserNameOrPasswordMessage.Text = "Please use a diffenrent username.       Phone number like expressions are not permitted as username"
                InvalidUserNameOrPasswordMessage.Visible = true;
                e.Cancel = true;
            }
            catch (ArgumentException)
            {

            }



它工作正常,如果有人使用作为用户名的7到10位数字,它不会继续,但会产生错误。



我试图在aspx页面的文本框中使用相同的逻辑。它似乎没有在那里工作。



我在代码隐藏文件的文本框中使用相同登录的代码是


it works fine and if any one uses 7 to 10 digits as username, it does not proceed but gives error in stead.

I am trying to use the same logic in a text box in an aspx page. It does not seem to work there.

My code for using the same login in text box in code behind file is

protected void Button1_Click(object sender, EventArgs e)
    {
        string regstr = "@^[1-9]{7,10}$";
        RegularExpressionValidator myreg = new RegularExpressionValidator();
        myreg.ValidationExpression = regstr;
        myreg.ControlToValidate = TextBox1.Text;
        try
        {
            myreg.Validate();
            ErrorLabel.Text = "Use min 7 and max 10 numbers";
        }
        catch (ArgumentException)
        {

        }
    }





虽然在CreatinguserAccount中使用相同的逻辑,但这不起作用。



若y使用myreg.VAlidate();我收到错误''ControlToValidate属性''不能为空'。



如果我使用myreg.Validate(TextBox1.Text);我收到错误'方法没有重载'验证'需要1个参数'



我使用了using指令'using System.Text.RegularExpressions;'但它确实似乎没有任何区别



显然我在使用代码进行验证时出现了一些错误。



请帮助我解决这个问题



另一个问题是我能够阻止使用像987654321这样的表达式作为用户名,但有人使用phone987654321,它仍然通过。我需要一个验证表达式,不仅可以防止7到10位数字,还可以防止任何表达式,包括7-10位数字。

请帮助我解决这两个问题



非常感谢您的帮助



This does not work although the same logic worked in CreatinguserAccount.

If y use myreg.VAlidate(); I get the error 'The ControlToValidate property of '' cannot be blank'.

and if I use myreg.Validate(TextBox1.Text); I get the error 'No overload for method 'Validate' takes 1 arguments'

I have used the using directive 'using System.Text.RegularExpressions;' but it does not seem to make any difference

Apparently I am making some error in using code behind for validation.

Kindly help me to resolve this problem

Another problem is that I have been able to prevent using expressions like 987654321 as username but someone uses phone987654321, it still passes through. I need a validation expression that prevents not only 7 to 10 digit number but any expression including 7-10 digit number.
Kindly help me in both these matters

Many thanks for your help

推荐答案

;
RegularExpressionValidator myreg = new RegularExpressionValidator();
myreg.ValidationExpression = regstr;
myreg.ControlToValidate = TextBox1.Text;
try
{
myreg.Validate();
ErrorLabel.Text =使用最小7和最多10个数字;
}
catch(ArgumentException)
{

}
}
"; RegularExpressionValidator myreg = new RegularExpressionValidator(); myreg.ValidationExpression = regstr; myreg.ControlToValidate = TextBox1.Text; try { myreg.Validate(); ErrorLabel.Text = "Use min 7 and max 10 numbers"; } catch (ArgumentException) { } }





虽然在CreatinguserAccount中使用相同的逻辑,但这不起作用。



如果y使用myreg.VAlidate();我收到错误''的ControlToValidate属性''不能为空'。



如果我使用myreg.Validate(TextBox1.Text);我收到错误'方法没有重载'验证'需要1个参数'



我用过了使用迪rective'using System.Text.RegularExpressions;'但它似乎没有任何区别



显然我在使用代码进行验证时出现了一些错误。 />


请帮我解决这个问题



另一个问题是我能够阻止使用像这样的表达式987654321作为用户名,但有人使用phone987654321,它仍然通过。我需要一个验证表达式,不仅可以防止7到10位数字,还可以防止任何表达式,包括7-10位数字。

请帮助我解决这两个问题



非常感谢您的帮助



This does not work although the same logic worked in CreatinguserAccount.

If y use myreg.VAlidate(); I get the error 'The ControlToValidate property of '' cannot be blank'.

and if I use myreg.Validate(TextBox1.Text); I get the error 'No overload for method 'Validate' takes 1 arguments'

I have used the using directive 'using System.Text.RegularExpressions;' but it does not seem to make any difference

Apparently I am making some error in using code behind for validation.

Kindly help me to resolve this problem

Another problem is that I have been able to prevent using expressions like 987654321 as username but someone uses phone987654321, it still passes through. I need a validation expression that prevents not only 7 to 10 digit number but any expression including 7-10 digit number.
Kindly help me in both these matters

Many thanks for your help


不是很好好主意,因为只是不使用电话号码不会产生密码字符串,但是...



所有你需要的是制定一些相似性的标准电话号码和密码。我会建议,例如,以下一个:只考虑数字。首先仅从电话号码中提取数字。然后,在一个循环中,按字符扫描整个将要成为密码的字符,并忽略非数字,将每个数字与电话号码进行比较,重复它直到在电话或密码候选中扫描所有数字,以先到者为准。计算巧合数。使用此计数作为密码错误的标准。



这是正则表达式无用的情况之一。尝试在执行验证的任何地方使用它只不过是思维的惯性。



-SA
Not a very good idea, because just not using a phone number won't make a password string, but…

All you need is to develop some criterion of "similarity" between the phone number and the password. I would suggest, for example, the following one: consider only digits. Extract only the digits from the phone number first. Then, in a loop, scan the whole would-be-a-password character by character, and, ignoring non-digits, compare each digits with a phone's digit, repeating it until all digits are scanned in a phone or a password candidate, whichever comes first. Count number of coincidences. Use this count as a criterion for a bad password.

This is one of the cases where the Regex is not helpful. An attempt to use it everywhere where "validation" is performed is nothing but some inertia of thinking.

—SA


我不会在这里讨论逻辑。但我想告诉你为什么你面临现有代码的问题。

I won't talk about logic here. But I would like to tell you why you are facing problems with the existing code.
引用:

如果y使用myreg.VAlidate();我收到错误''ControlToValidate属性''不能为空'。

如果我使用myreg.Validate(TextBox1.Text);我收到错误'没有方法重载'验证'需要1个参数'

If y use myreg.VAlidate(); I get the error 'The ControlToValidate property of '' cannot be blank'.
and if I use myreg.Validate(TextBox1.Text); I get the error 'No overload for method 'Validate' takes 1 arguments'

那是因为你已经指定 TextBox Text as ControlToValidate 属性。相反,你应该只提供 TextBox ID ... ...

That is because you have assigned TextBox Text as ControlToValidate Property. Instead you should provide only the TextBox ID like...

myreg.ControlToValidate = "TextBox1";



参考 - BaseValidator.ControlToValidate Property [ ^ 。您还可以在 aspx 页面中声明验证器。这很简单。



对于逻辑,研究更多并尝试别的。


Refer - BaseValidator.ControlToValidate Property[^]. You can also declare validator in aspx page. That is easy.

For the logic, research more and try something else.


这篇关于防止文本框中的电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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