User Name Regular Expression Pattern
for more regular expression syntax refer Java Regular Expression Syntax Descriptions
UserNameValidator.java
Output :
^[a-z0-9]{5,15}$Regular ExpressionDescription
for more regular expression syntax refer Java Regular Expression Syntax Descriptions
UserNameValidator.java
package com.javatutorialcorner.regex;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UserNameValidator {
private static final String PATTERN = "^[a-z0-9]{5,15}$";
public static void main(String[] args) {
System.out.println("Validate jtc123 : "+validate("jtc123"));
System.out.println("Validate JTC123 : "+validate("JTC123"));
System.out.println("Validate Jtc45 : "+validate("Jtc45"));
System.out.println("Validate 123Jtc : "+validate("123Jtc"));
System.out.println("Validate jtc : "+validate("jtc"));
System.out.println("Validate jtcjtc : "+validate("jtcjtc"));
System.out.println("Validate 123jtc : "+validate("123jtc"));
}
private static boolean validate(String userName) {
Pattern pattern = Pattern.compile(PATTERN);
Matcher matcher = pattern.matcher(userName);
return matcher.matches();
}
}
Output :
Validate jtc123 : true
Validate JTC123 : false
Validate Jtc45 : false
Validate 123Jtc : false
Validate jtc : false
Validate jtcjtc : true
Validate 123jtc : true




0 comments:
Post a Comment