用户名验证 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31589050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Username Validation Java
提问by Rodrigo G Rodrigues
I'm new in Java, so I don't know very good the language. I have a simple HTML Form to fill register for Login.
我是 Java 新手,所以我不太了解这门语言。我有一个简单的 HTML 表单来填写登录注册。
My problem is a detail in the username, it can't have some invalid character (accents and symbols, for example) and I don′t know how to check the username characters.
我的问题是用户名中的一个细节,它不能有一些无效字符(例如重音和符号),我不知道如何检查用户名字符。
I used request.getParameter("username")
to get username in a String
variable.
我曾经request.getParameter("username")
在String
变量中获取用户名。
String username = request.getParameter("username");
How can I proceed?
我该如何继续?
回答by Tobías
A simple way is the String#matches(String regex)
function:
一个简单的方法是String#matches(String regex)
函数:
boolean matches(String regex)
Tells whether or not this string matches the given regular expression.
boolean match(String regex)
判断此字符串是否与给定的正则表达式匹配。
String username = request.getParameter("username");
boolean valid = (username != null) && username.matches("[A-Za-z0-9_]+");
but if this is to be used multiple times is more efficient to use a Pattern
:
但如果要多次使用它,则使用更有效Pattern
:
Pattern pattern = Pattern.compile("[A-Za-z0-9_]+");
and use it each time:
并每次使用它:
boolean valid = (username != null) && pattern.matcher(username).matches();
回答by Ankur Pathak
Use this Bean Validation library:
使用这个 Bean 验证库:
https://github.com/ankurpathak/username-validationhttps://mvnrepository.com/artifact/com.github.ankurpathak.username/username-validation
https://github.com/ankurpathak/username-validation https://mvnrepository.com/artifact/com.github.ankurpathak.username/username-validation
It has all constraints for google like username validation and many more.
它具有谷歌的所有限制,如用户名验证等等。
Library has different constraint to deal with username validation:
库有不同的约束来处理用户名验证:
- UsernamePattern to allow a-z,0-9,period and underscore characters. These characters can we turned off with some flags in constraints like includePeriod, includeUnderscore and useDigit
- EndWithAlphabet to check if username end with alphabet
- StartWithAlphabet to check if username start with alphabet
- StartWithAlphaNumeric to check username start with alphanumeric
- EndWithAlphaNumeric to check username end with alphanumeric
- NotContainConsecutivePeriod to check username not contain consecutive period
- NotContainConsecutiveUnderscore to check username not contain consecutive underscore
- NotContainPeriodFollowedByUnderscore to check username not contain period followed by underscore
- NotContainUnderscoreFollowedByPeriod to check username not contain underscore followed by period
- UsernamePattern 允许使用 az、0-9、句点和下划线字符。我们可以使用 includePeriod、includeUnderscore 和 useDigit 等约束中的一些标志来关闭这些字符
- EndWithAlphabet 检查用户名是否以字母结尾
- StartWithAlphabet 检查用户名是否以字母开头
- StartWithAlphaNumeric 检查用户名以字母数字开头
- EndWithAlphaNumeric 检查用户名以字母数字结尾
- NotContainConsecutivePeriod 检查用户名不包含连续句点
- NotContainConsecutiveUnderscore 检查用户名不包含连续下划线
- NotContainPeriodFollowedByUnderscore 检查用户名不包含句点后跟下划线
- NotContainUnderscoreFollowedByPeriod 检查用户名不包含下划线后跟句点
All the constraints by default ignore blank so that it will be reposted separately by NotBlank standard bean validation constraint and same can we turned of using ignoreBlank(true by default) flag of each constraint. So google like username can be achieved by:
默认情况下,所有约束都忽略空白,以便它会被 NotBlank 标准 bean 验证约束单独重新发布,同样我们可以使用每个约束的 ignoreBlank(默认为真) 标志。因此可以通过以下方式实现类似 google 的用户名:
@UsernamePattern
@StartWithAlphaNumeric
@NotContainConsecutivePeriod
@EndWithAlphaNumeric
@NotBlank
private String username;