We are moved to new domain
Click -> www.ehowtonow.com
Friday, 10 March 2017

Validate Username using Java Regular Expression

User Name Regular Expression Pattern
^[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

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Validate Username using Java Regular Expression Rating: 5 Reviewed By: eHowToNow