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