2013-05-09 60 views
0

使用Spring表单验证来验证用户输入的输入字段。我需要帮助以在特定领域中包含空间。以下是我使用的验证注释。但它似乎不允许空间。Spring Validation using Annotation @RegExp

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

我想知道我需要什么样的价值在验证注释以包括在名称中包含

我想包括在EXP值“\ s”来包含空格的空间。但它似乎并不工作

任何帮助,这是非常赞赏。

回答

1

您的正则表达式字符串对您的需求无效。

使用以下的正则表达式来代替:

//([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")); 

} 
+0

为什么管'|'在字符类? – 2014-05-31 13:29:20

相关问题