Click here to Skip to main content
15,905,136 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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)
            {

            }

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)
        {

        }
    }


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
Posted
Updated 12-Feb-14 5:20am
v2
Comments
Ahmed Bensaid 12-Feb-14 11:28am    
Try to pass your validator in markup instead of in code behind, like this :
<asp:RegularExpressionValidator ID="REVtxtEMailId"
runat="server"
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
Display="Dynamic"
ErrorMessage="Invalid Email Format"
ForeColor="Red" ControlToValidate="txtEMailId">
Member 10235977 12-Feb-14 19:15pm    
It will validate an email. What if I want to prevent an email from being validated? this is exactly what I wan to do - prevent an email or phone number. that is why I used code behind and rejected whatever is accepted as valid email. I hope I have made my point clear. There is validation expression for validating email but there is no validation expression to prevent validating an email. that is why I said I used the validation in reverse order, that is rejected whatever was accepted as a valid email id or phone number and accepted the exceptions that were not validated.

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
 
Share this answer
 
I won't talk about logic here. But I would like to tell you why you are facing problems with the existing code.
Quote:
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'
That is because you have assigned TextBox Text as ControlToValidate Property. Instead you should provide only the TextBox ID like...
C#
myreg.ControlToValidate = "TextBox1";

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

For the logic, research more and try something else.
 
Share this answer
 
Comments
Member 10235977 12-Feb-14 19:08pm    
I have already tried using TextBix1 for specifying ControlToValidate. It gives an error 'cannot implicitly convert type 'System.Web.UI.WebControl.TextBox to string'
Declaring a validation in aspx page can validate a number. I want to prevent numbers. So I have to reject the text that is validated and accept the text that is not validated. This is what I meant by saying I used validation in reverse order. sorry if it has been misunderstood. I could not come across any validation expression that will prevent a 7-10 digit number so that I can use it in the aspx page.
I am sure you are trying like...

myreg.ControlToValidate = TextBox1; // This will throw you Exception

See my answer, I have given you something different...

myreg.ControlToValidate = "TextBox1"; // It is string.
Member 10235977 13-Feb-14 8:47am    
Thanks. My code now 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";
try
{
myreg.Validate();

}
catch (ArgumentException)
{
ErrorLabel.Text = "Use min 7 and max 10 numbers";
}
}

whatever I type in TextBox1, I get the error
'Object reference not set to an instance of an object'.

it is almost done with your help. Only this one is to be sorted out.
Kindly help once more

Many thansk
On which line exactly it is throwing this Exception? Can you check while debugging?
Member 10235977 13-Feb-14 21:07pm    
Line 27: myreg.Validate();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900