0

Why does this return false for any normal word I enter?

if(!guestbook.getName().matches("[a-zA-Z0-9\\s]")) {
        errors.rejectValue("name", "stringFormat.falseCharacters", "You are only allowed to use numbers, letters and spaces for the name.");
    }

I must be missing something.

5 Answers 5

1

Your regex only matches one-character long Strings.
To match one or more number of characters change it to:

"[a-zA-Z0-9\\s]+"

You might also find this (imo great) Regular-Expressions reference useful.

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

Comments

1

What you need is [a-zA-Z0-9\\s]+. Add + at the end.

Comments

0

Because you should add the '+' at the end of your regex, otherwise you are requesting the match of just one character.

Comments

0

In regular expressions you use the + at the end of your range to specify "more than one" while in your case you probe for only for expressions with a length of one.

Comments

0

Try this regex "^[A-Za-z0-9\s]+$",

if(!guestbook.getName().matches("^[A-Za-z0-9\\s]+$")) {
    errors.rejectValue("name", "stringFormat.falseCharacters", "You are only allowed to use numbers, letters and spaces for the name.");
}

Comments

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.