im pretty new in grails.. im having a little problem right now on validation using matches. What I wanted to happen is that a field can accept combination of alphanumeric and special characters, letters only and numbers only, and if the user inputs special characters only, the system should prompt the user an error. i used matches constraints to validate the data, and im having a hard time how could i set the regex where the field will not accept an input with special characters only. please help me.. thanks a lot for sharing your knowledge.
regex validation - grails
6k Views Asked by antibry At
2
There are 2 best solutions below
0

I don't know if grails supports lookaround, but if it does, this regex will work for you:
/(?=^[\pL\pN!:;]+$)(?!^[!:;]+$)/
explanation:
/ : regex delimiter
(?= : begin positive lookahead
^ : start of string
[\pL\pN!:;]+ : a letter, a digit or a special char one or more times
$ : end of string
) : end of lookahead
(?! : begin negative lookahead
^ : start of string
[!:;]+ : a special char one or more times
$ : end of string
) : end of lookahead
/ : regex delimiter
I think I understand your problem, but correct me if I'm wrong. The input is valid as long as there is at least 1 letter or number, right? In other words, if there isn't a letter or number (only special characters), then the input is invalid?
See if this works:
Here is my little groovy test:
Explained: