Regex Error - unrecognized escape sequence

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to validate input from a textbox in the code-behind of my page.
I don't want to use any validator controls for this, I'd like to do it all in
the code behind. However, I'm getting an "unrecognized escape sequence"
compile error. Can someone please help me determine the correct regular
expression statement? I'm trying to validate that the textbox only contains
"Safe" characters (digits, letters, spaces, periods, etc).

Here is my code:

//Validate for safe characters
Regex r = new Regex("^[a-zA-Z0-9\\s.\\-]+$");

if (!r.IsMatch(txtUVNONumber.Text))
{
lblErrorMessage.Text = "Invalid entry for UVNO Number";
lblErrorMessage.Visible = true;
}
 
PK9

Did you try this already

Regex r = new Regex(@"^[a-zA-Z0-9\\s.\\-]+$");

I hope this helps,

Cor
 
Back
Top