An Article About Validations in Password

Posted By : Rupesh Sharma | 30-Apr-2018

Testing application for bugs and errors is a decent task so programmer needs to be clever enough to peep through the loopholes of the program and insert adequate validation in order to make his program bug-free. Like when we design some user-friendly standalone application in SpringBoot we have the variously predefined annotation to perform certain validation but annotation have certain limitation like we do not have the annotation for every particular field. In that case, we need some external help to code down the required validations for the program.

Let's take the case of simple password validation where it is very important that user must enter the right data otherwise password could be discarded. So first we gonna discuss what all validation do we need to require to make the code more bug-free and then we will move onto their solutions.

Various points that we need to keep in mind:

  • Leading and trailing spaces should be removed.
  • No spaces should be there in between in the password even.
  • Must accept certain rules according to which password should be created like accepting one Upper case value, one Lower case value,one Numeric value,size should be between 8 to 32.
  • Should not be left null.

These are some important validation that should be in your code to improve the quality of the code for the password field. Now we will look at the snippet for the required validation.

Let's say we have a model class UserSignUp.java


 

package com.example.model;

import javax.persistence.CascadeType;

import javax.persistence.Entity;
import javax.persistence.Id;

import com.example.enums.UserStatus;

@Entity    
@Table(name="UserTable")
public class User
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer userId
    private String userName;
   
    private String email;
    
   
    private String phoneNumber;
   
    private String password;
   
    private String country;
    private String createdOn;
    

}

So we have a field like these then the code we need to use is:

According to  model there are several fields but here we will focus on password in the below code to  avoid cumbersomeness.

 


 

        @RequestMapping(value="/signup", method=RequestMethod.POST)
    public String insertUser(@RequestBody User user)
    { 
        
            String passwordvalue=user.getPassword();
            int passwordLength1=passwordvalue.length();
            passwordvalue=passwordvalue.replaceAll("\\s+","");
            int passwordLength2=passwordvalue.length();

          // THE ABOVE FOUR LINE MAKE SURE THAT LEADING SPACES AND TRAILING SPACES SHOULD BE OF DIFFERENT LENGTH 
        
           String pattern="(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{8,32}";  // HERE ONE UPPER CASE VALUE, ONE LOWER CASE VALUE,ONE       NUMERIC VALUE , ONE SPECIAL CHARACTER IS THERE WITH LENGTH 8 TO 32.
           if((passwordLength2!=0) && (passwordLength1==passwordLength2) && (passwordvalue.matches(pattern)))
        {
                     String u=signupservice.addUser(user);// HERE WE CAN SAVE THE INFORMATION IN REPOSITORY
                     return "Your account has been successfully created ";
            }          
                   else
             return "enter valid password";

                        
     }
    

Here we have seen that how validation can be implemented in the password field using this java code.

About Author

Author Image
Rupesh Sharma

Rupesh Sharma is a trainee here as an assistant consultant developer. His core interest is in java and posses good analytical and logical skills. He likes to learn new technology and will be to glad to get feedbacks for improvement.

Request for Proposal

Name is required

Comment is required

Sending message..