Restrict text field to accept only numbers and dots


Badge +2

Hi I would like to restrict the field with numbers and dots(.) using RegEx expressions.

Please guide me. For example, this is actually an IP Address

10.111.123.23
10.111.151.150


3 replies

Userlevel 5
Badge +14

try eg.

^(?:(25[0-5]|2[0-4][0-9]|[01]?[0-9]{1,2})(.(?!$)|$)){4}$
Badge +2

Hi Marian

It should not accept this also, please refer below screen shot. The above regex is accepting two digits after first dot/decimal, it should not accept. The format should be like this. xx.xxx.x/xx/xxx.x/xx/xxx . Here "/" is OR.

What about this - ^([0-9]{2}.[0-9]{3}.[0-9]{1,3}.[0-9]{1,3})$

215589_pastedImage_1.png

Userlevel 5
Badge +14

then I'd define the pattern like follows

^[0-9]{2}.(25[0-5]|2[0-4][0-9]|[01][0-9]{2}).(25[0-5]|2[0-4][0-9]|[01]?[0-9]{1,2}).(25[0-5]|2[0-4][0-9]|[01]?[0-9]{1,2})$

your pattern would match as well input like 10.999.999.999 which is not valid IP address ...

Reply