siva636
siva636

Reputation: 16441

Regular expression which prevents certain characters and repeated characters

I am trying to validate a user input in my JSF application where the user

(1) should not enter their email address (that simply means the '@' character should not come there)

(2) should not enter their phone number (we may simply reject if the input contains minimum of 4 digit number input)

(3) should not keep on pressing the same button (we may reject if the input contains minimum of 4 repeated characters)

I need a regular expression to do the above validation.

Upvotes: 0

Views: 997

Answers (2)

codaddict
codaddict

Reputation: 454922

You can use negative lookahead assertions for this as:

^(?!.*@)(?!.*\d{4})(?!.*?(.)\1{3}).*$

Rubular link

Upvotes: 2

Michael Low
Michael Low

Reputation: 24506

I think here it's easiest to match based on what you can't accept rather than what you can. The regex @|\d{3}\d+|(.)\1{3} is quite simple and easy to understand, and you can reject any input that that matches it.

Upvotes: 2

Related Questions