TheKingPinMirza
TheKingPinMirza

Reputation: 8912

ng-pattern regex to allow all characters except decimal value

I have come up with the following regex sample to allow everything from keybaord except float(decimal value) e.g. .1, 0.1, 1.1, 1.1., 1.1.1 etc

ng-pattern="/^[\w -!@#$%^&\*()\+]*$/" />  

Is there a short way to write regex for my requirement i.e. Allow everything from keyboard except loat(decimal value) e.g. 0.1, 1.1, 1.1. etc?

Upvotes: 0

Views: 902

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174696

Use a negative lookahead based regex.

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

(?!...) called negative lookahead which asserts that the match won't be followed by the text which was matched by the pattern present inside the negative lookahead. So here, ^(?!\d+\.\d+$) , it matches the start of the line boundary only if the start of the line boundary was not followed by a \d+\.\d+$ decimal number.

DEMO

Upvotes: 1

vks
vks

Reputation: 67968

^[\w !@#$%^&\*()\+-]*$

    ^^            ^^

Your regular expression cannot accept decimals as it cannot match ..You need to place - at the end or escape it/

Upvotes: 0

Related Questions