1

I have the following issue:

User.groovy

class Users {

String userName
String passwd

static constraints = {
    userName(nullable:false, maxSize:20)
    passwd(password:true,
            validator: {val, obj, errors->

                if (!(obj.passwd.matches("(.*[\\d])"))) errors.rejectValue('passwd', 'noNumber')

                if (!obj.passwd.matches("(.*[\\W])")) errors.rejectValue('passwd', 'noSpecialCh')

                if (!obj.passwd.matches("(.*[a-z])")) errors.rejectValue('passwd', 'noLower')

                if (!obj.passwd.matches("(.*[A-Z])")) errors.rejectValue('passwd', 'noUpper')

            }
    )
}

}

messages.properties

noNumber.racetrack.Users.passwd=password should contain at least one number noSpecialCh.racetrack.Users.passwd=password should contain at least one special character noLower.racetrack.Users.passwd=password should contain at least one lower case letter noUpper.racetrack.Users.passwd=password should contain at least one upper case letter

These lines of code give me error messages.

However,

when i enter "1222s" in password it gives the error messages: password should contain at least one number password should contain at least one special character password should contain at least one upper case letter

even though i have entered number.

when i enter "sss1" in password it gives the error messages: password should contain at least one lower case letter password should contain at least one special character password should contain at least one upper case letter

even though i have entered some lower cases.

It seems as if it checks the last character in the end and gives error messages.

What am i doing wrong in User.groovy or messages.properties? Are my Regular expressions wrong?

Please help me.

1 Answer 1

0

Your regexes are wrong. It should be \d+ for number, \W+ for special character, [a-z]+ and so on. Special symbols are escaped with one slash. + means - one or more occurrences of pattern. No need for .* and grouping with ().

Sign up to request clarification or add additional context in comments.

1 Comment

doing that did not help :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.