Tomasz Nazarenko
Tomasz Nazarenko

Reputation: 1194

Regex, how to disallow leading zeros in whole numbers and allow one leading zero in fractions?

The ^[0-9]{1,12}((\\.|,)[0-9]{0,2})?$ regex works ok, except I do not want it two match it against a case, where there are two leading zeros. How to write it?

This shouldn't be allowed:

0011
00,1
01,11
02 etc.

but this is OK 0,22, 0

Upvotes: 0

Views: 191

Answers (1)

Nahuel Fouilleul
Nahuel Fouilleul

Reputation: 19315

changing

^[0-9]{1,12}

by

^[1-9][0-9]{0,11}

will force number to start with a digit between 1 and 9 included

following question edit, to accept 0,22:

^(0|[1-9][0-9]{0,11})((\\.|,)[0-9]{0,2})?$

Upvotes: 1

Related Questions