Jean Tehhe
Jean Tehhe

Reputation: 1357

Regex get error in textbox when having space at the end

I'm using the following regex for validation of numbers which is working fine.
Problem is that if you enter in the textbox some valid number and then press space, the expression is not valid anymore.
How should I ignore spaces at the end of the entered value by regex handling ?

@"^[0-9]+$"

Upvotes: 0

Views: 61

Answers (2)

Zohar Peled
Zohar Peled

Reputation: 82534

There are at least 2 ways to solve this problem:

  1. add \s* to the regEx - @"^[0-9]+\s*$" - it means any number of white spaces of any kind are allowed after at least one digit

  2. Use the regEx on the trimmed text of the textbox - regEx.IsMatch(TextBox.Text.Trim())

Upvotes: 0

aelor
aelor

Reputation: 11126

@"^[0-9]+\s*$"

like this you can make the space take zero or more spaces at the end.

* is a quantifier made for this particular purpose

Upvotes: 1

Related Questions