simple textbox validation using regexvalidator

  • Thread starter Thread starter live your lives
  • Start date Start date
L

live your lives

i am trying to validate a simple username textbox using RegularExpressionValidator:

TextBox tbUserName = new TextBox();
tbUserName.ID = "tbUserName";

string strPatternUserName = @"\W";
// i've tried using "\\W", "\w", "\\w","@\w", "/\\w", etc...
// but it always prints my error msg UNLESS the textbox is set to "".
// why does this not work?

RegularExpressionValidator rev = new RegularExpressionValidator();
rev.ControlToValidate = "tbUserName";
rev.Text = "Please enter a username";
rev.ValidationExpression = strPatternUserName;
rev.Display = ValidatorDisplay.Static;
rev.EnableClientScript = false;

please help, thank you.
 
live your lives said:
i am trying to validate a simple username textbox using RegularExpressionValidator:

TextBox tbUserName = new TextBox();
tbUserName.ID = "tbUserName";

string strPatternUserName = @"\W";
// i've tried using "\\W", "\w", "\\w","@\w", "/\\w", etc...
// but it always prints my error msg UNLESS the textbox is set to "".
// why does this not work?

The following method returns true:

private static bool TestRegex()
{
bool match = false;
string test = @"\William";

Regex expr = new Regex("^(\\\\W)");
match = expr.IsMatch(test);

return match;
}

When in doubt, use more backslashes.

- carl
 
The problem with the regularexpressionvalidator control is that it expects a
string, not a RegEx expression. Since they are of different types I cannot
pass a RegEx object into the regularexpressionvalidator control.

Anyhow, I got it working similar to the way you did it. It is rather
strange that more backslashes are used. Ah well, at least it works right.

Thanks for your help.
 
Back
Top