-1

I have a textbox it should only allow only one dot and atleast one digit after the dot.

for eg: 1 and 1.2. If i enter 1. it should take 1.

33.0, 33, 55.23, 55.6,

i tried the following regex but it is not allowing dot.

It should allow only one dot in the textbox along with digits.

Geeth
  • 5,282
  • 21
  • 82
  • 133

1 Answers1

1

To ensure that your textbox can only contain a digit, optionally followed by a single period and 1 or more digits, use:

^\d+(\.\d+)?$

To also allow for an optional trailing or leading space, use:

^ ?\d+(\.\d+)? ?$
Zak
  • 1,042
  • 6
  • 12
  • 1
    And what about `23.65` ? Add a quantifier after the first `\d`, like `\d+`. – Jan May 27 '18 at 17:12
  • Regex regex = new Regex("^\\d+(\\.\\d+)?$"); Its not allowing dot. – Geeth May 27 '18 at 17:40
  • private void _textBoxPreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e) { Regex regex = new Regex("^\\d+(\\.\\d+)?$"); e.Handled = !regex.IsMatch(e.Text); } – Geeth May 27 '18 at 17:42
  • @Geeth I'm sorry, but I can't test in wpf. The regex provided will match `1`, `1.2`,`33.0`, `33`, `55.23` and `55.6`. Do you need it to match just `.`? – Zak May 27 '18 at 19:30