[英]Java Spring form check whether a input string contains special characters or not
I am trying to create a Java Spring form that checks whether the input string contains special characters or not.我正在尝试创建一个 Java Spring 表单来检查输入字符串是否包含特殊字符。 For example when a user types in a string containing special characters a message will pop up stating that the input string contains special characters.
例如,当用户输入包含特殊字符的字符串时,会弹出一条消息,指出输入字符串包含特殊字符。
My question is how to implement java function to check for special characters in the input string?我的问题是如何实现java函数来检查输入字符串中的特殊字符?
Model模型
package com.mkyong.customer.model; public class Customer { String userName; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
Controller控制器
package com.mkyong.customer.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.validation.BindException; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.SimpleFormController; import com.mkyong.customer.model.Customer; public class TextBoxController extends SimpleFormController{ public TextBoxController(){ setCommandClass(Customer.class); setCommandName("customerForm"); } @Override protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { Customer customer = (Customer)command; return new ModelAndView("CustomerSuccess","customer",customer); } }
CustomerForm客户表格
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <html> <head> <style> .error { color: #ff0000; } .errorblock{ color: #000; background-color: #ffEEEE; border: 3px solid #ff0000; padding:8px; margin:16px; } </style> </head> <body> <h2>Java Textbox Special Characters</h2> <form:form method="POST" commandName="customerForm"> <form:errors path="*" cssClass="errorblock" element="div"/> <table> <tr> <td>Keyword:</td> <td><form:input path="userName" /></td> <td><form:errors path="userName" cssClass="error" /></td> </tr> <tr> <td colspan="3"><input type="submit" /></td> </tr> </table> </form:form> </body> </html>
CustomerSuccess客户成功
<html> <body> <h2>Java Textbox Special Characters</h2> Keyword: ${customer.userName} </body> </html>
You can have a list of all special characters and check it with regex:您可以拥有所有特殊字符的列表并使用正则表达式进行检查:
Pattern regex = Pattern.compile("[$&+,:;=\\\\?@#|/'<>.^*()%!-]"); // fill in any chars that you consider special
if (regex.matcher(stringToCheck).find()) {
// found
}
You can also look into Guava's CharMatcher
(no regex and more readable).您还可以查看
Guava's CharMatcher
(没有正则表达式且更具可读性)。 You can construct your own matching logic like so:您可以像这样构建自己的匹配逻辑:
boolean isAMatch = CharMatcher.WHITESPACE
.or(CharMatcher.anyOf("[$&+,:;=\\\\?@#|/'<>.^*()%!-]")) // fill in any chars that you consider special
.matchesAnyOf(stringToCheck);
Separate method for convert special character特殊字符的单独转换方法
if(checkSpecialChar("^[^<>{}\"/|;:.,~!?@#$%^=&*\\]\\\\()\\[¿§«»ω⊙¤°℃℉€¥£¢¡®©0-9_+]*$", source){
// add your staff here
}
public boolean checkSpecialChar(String regex, String source) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(source);
return matcher.find();
In Spring you can get the value of your Form like this:在 Spring 中,您可以像这样获取表单的值:
@RequestMapping(value = "/", method = RequestMethod.POST)
public String submit(@Valid @ModelAttribute("userName")String username){
// And now you can check with the regex pattern from hovanessyan if it contains special characters
Pattern regex = Pattern.compile("[$&+,:;=\\\\?@#|/'<>.^*()%!-]");
if (regex.matcher(username).find()) {
// found
}
}
I hope it helps.我希望它有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.