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

问题描述

我想阻止用户使用他们的电话号码作为用户名。所以我用后面的代码进行验证。如果用户名验证为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

推荐答案





虽然在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



引用:

如果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";



参考 - [。您还可以在 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.


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

08-23 01:13
查看更多