JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to validate password using regular expression?


Description:

The below given example shows how to validate a password using regular expression. Here this regular expression allows must contain one digit, one lower case char, one upper case char, some special chars, length should be within 6 to 15 chars.


Code:
package com.java2novice.regex;

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

public class MyPasswordValidate {

private static Pattern pswNamePtrn = 
		Pattern.compile("((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,15})");
	
	public static boolean validatePassword(String userName){
		
		Matcher mtch = pswNamePtrn.matcher(userName);
		if(mtch.matches()){
			return true;
		}
		return false;
	}
	
	public static void main(String a[]){
		System.out.println("Is 'java2novice' a valid password? "
							+validatePassword("java2novice"));
		System.out.println("Is 'gabbarsingh' a valid password? "
							+validatePassword("gabbarsingh"));
		System.out.println("Is 'Java2NOVICE$' a valid password? "
							+validatePassword("Java2NOVICE$"));
		System.out.println("Is '234aBc#' a valid password? "
							+validatePassword("234aBc#"));
	}
}

Output:
Is 'java2novice' a valid password? false
Is 'gabbarsingh' a valid password? false
Is 'Java2NOVICE$' a valid password? true
Is '234aBc#' a valid password? 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
When to use LinkedList or ArrayList?
Accessing elements are faster with ArrayList, because it is index based. But accessing is difficult with LinkedList. It is slow access. This is to access any element, you need to navigate through the elements one by one. But insertion and deletion is much faster with LinkedList, because if you know the node, just change the pointers before or after nodes. Insertion and deletion is slow with ArrayList, this is because, during these operations ArrayList need to adjust the indexes according to deletion or insetion if you are performing on middle indexes. Means, an ArrayList having 10 elements, if you are inserting at index 5, then you need to shift the indexes above 5 to one more.
Famous Quotations
Insanity: doing the same thing over and over again and expecting different results.
-- Albert Einstein

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 © 2018 by Nataraja Gootooru. All Rights Reserved.