-3

I want A regular Expression to validate this input of numeric(3,2)

like 2.00 , 7.96 or 9.27

What Should I try ?

Andrew_Nady
  • 91
  • 1
  • 1
  • 4

2 Answers2

2
^\d\.\d{2}$

This one will match the number EXACTLY in the format where is one digit before dot and two digits after it.

^\d+\.\d{2}$

This one will match the number, where is an arbitrary number of digits before the dot and two digits after it.

^\d+\.\d+$

This one will match the number, where the number of digits before and after the dot is completely arbitrary.


Notation explanation

^ - beginning of a line

$ - end of a line

+ - the preceding symbol must occur once or more times

\d - a decimal number

. - the escaped dot symbol - otherwise it's a special character

Community
  • 1
  • 1
Eenoku
  • 2,741
  • 4
  • 32
  • 64
0
^\d+?\.\d{2}$

This will look for d.dd where the first d is optional (if they enter just .12 for example). The $ at the end indicates the end of the string, so if they start typing letters or additional characters at the end of the string it won't match and you can throw an error for invalid input.

user2247281
  • 143
  • 8
  • \d+ will match multiple digit values, but if you want numeric(3,2) then you can remove the + to restrict it to just one digit. – user2247281 May 10 '15 at 16:38