JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to validate user name using regular expression?


Description:

The below given example shows how to validate a user name. The regular expression pattern allows lower case alphanumeric characters, allows '-', '_'. Incase if you want to support uppercase characters then regular expression should be ^[a-zA-Z0-9_-]{6,14}$


Code:
package com.java2novice.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyUsernameValidate {

	private static Pattern usrNamePtrn = Pattern.compile("^[a-z0-9_-]{6,14}$");
	
	public static boolean validateUserName(String userName){
		
		Matcher mtch = usrNamePtrn.matcher(userName);
		if(mtch.matches()){
			return true;
		}
		return false;
	}
	
	public static void main(String a[]){
		System.out.println("Is 'java2novice' a valid user name? "
						+validateUserName("java2novice"));
		System.out.println("Is 'cric' a valid user name? "
						+validateUserName("cric"));
		System.out.println("Is 'JAVA2NOVICE' a valid user name? "
						+validateUserName("JAVA2NOVICE"));
		System.out.println("Is 'java.2.novice' a valid user name? "
						+validateUserName("java.2.novice"));
		System.out.println("Is 'java_2-novice' a valid user name? "
						+validateUserName("java_2-novice"));
	}
}

Output:
Is 'java2novice' a valid user name? true
Is 'cric' a valid user name? false
Is 'JAVA2NOVICE' a valid user name? false
Is 'java.2.novice' a valid user name? false
Is 'java_2-novice' a valid user name? true
<< Previous Program | Next Program >>

List Of Regular Expression Sample Programs:

  1. Simple regex pattern matching using string matches().
  2. Simple java regex using Pattern and Matcher classes.
  3. Java regex with case insensitive.
  4. How to validate IP address using regular expression?
  5. How to replace a pattern using regular expression in java?
  6. How to remove multiple spaces with a single space with in a string?
  7. How to validate user name using regular expression?
  8. How to validate email address using regular expression?
  9. How to validate password using regular expression?
  10. How to validate file extensions using regular expression in java?
  11. How to validate date format using regular expression in java?
  12. How to capture or extract a value(s) from text using regular expression in java?
  13. How to split a string using regular expression?
Knowledge Centre
Domain Naming Service(DNS)
It is very difficult to remember a set of numbers (IP address) to connect to the Internet. The Domain Naming Service(DNS) is used to overcome this problem. It maps one particular IP address to a string of characters. For example, www.java2novice.com implies com is the domain name reserved for US commercial sites, java2novice is the name of the company and www is the name of the specific computer, which is java2novice's server.
Famous Quotations
If you don’t make mistakes, you’re not working on hard enough problems. And that’s a big mistake.
-- Frank Wilczek

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.