0

Im using Spring form validation to validate the input fields entered by the user. I need help to include space in a particular field. Below is the validation annotation that Im using. But it does not seem to allow space.

@RegExp(value="([0-9|a-z|A-Z|_|$|.])*",message="value can contain only digits,alphabets or _ or . or $")
private String cName ;

I would like to know what value I need to include in the validation annotation to include space in the name

I tried to include '\s' in the exp value to include blank space. But it doesn't seem to work

Any help on this is much appreciated.

1 Answer 1

1

Your regex String is not valid for your requirement.

Use the following regex instead:

 //([0-9|a-z|A-Z|\\_|\\$|\\.|\\s])+

@Test
public void testRegex() {
    String r = "([0-9|a-z|A-Z|\\_|\\$|\\.|\\s])+";
    assertTrue("Allows space", Pattern.matches(r, "test test"));
    assertTrue("Allows .", Pattern.matches(r, "12My.test"));
    assertTrue("Allows _", Pattern.matches(r, "My_123"));
    assertTrue("Allows $", Pattern.matches(r, "$ 1.0"));

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

1 Comment

Why the pipes | in the character class?

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.