Username Validation Java -
i'm new in java, don't know language. have simple html form fill register login.
my problem detail in username, can't have invalid character (accents , symbols, example) , don´t know how check username characters.
i used request.getparameter("username")
username in string
variable.
string username = request.getparameter("username");
how can proceed?
a simple way string#matches(string regex)
function:
boolean matches(string regex)
tells whether or not string matches given regular expression.
string username = request.getparameter("username"); boolean valid = (username != null) && username.matches("[a-za-z0-9_]+");
but if used multiple times more efficient use pattern
:
pattern pattern = pattern.compile("[a-za-z0-9_]+");
and use each time:
boolean valid = (username != null) && pattern.matcher(username).matches();
Comments
Post a Comment