simple textbox validation using regexvalidator

  • Thread starter live your lives
  • 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.
 
V

Vagabond Software

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
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top