开发者

Regular expression which prevents certain characters and repeated characters

开发者 https://www.devze.com 2023-01-28 07:39 出处:网络
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 \'@\' cha开发者_开发技巧racter should not come there)

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 '@' cha开发者_开发技巧racter 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.


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.


You can use negative lookahead assertions for this as:

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

Rubular link

0

精彩评论

暂无评论...
验证码 换一张
取 消