JavaScript

JavaScript Regular Expression Password Validation Having Special Characters

Password validation is important for the security and integrity of sensitive information. A strong and secure password contains letters, numbers, and special characters. Special characters can help to make a password unique. The use of special characters in passwords is encouraged because it helps to make the password stronger and less susceptible to cracking. This makes the password less likely to be found in a list of commonly used passwords, which are frequently targeted by attackers.

This article will describe the regular expression for the validation of a password that contains special characters.

How to Write the JavaScript Regular Expression Password Validation Having Special Characters?

Use the following pattern or regular expression for password validation containing special characters . The pattern will match a password that contains at least one special character, one letter, and one digit, and is between 8 and 12 characters in length:

regexPattern = /^(?=.*[-\#\$\.\%\&\@\!\+\=\<\>\*])(?=.*[a-zA-Z])(?=.*\d).{8,12}$/;

Here’s the breakdown of the different parts of the above pattern:

  • ^” represents the start of the string or pattern.
  • (?=.*[-\#\$\.\%\&\@\!\+\=\<\>\*])” matches any string that contains at least one special character, listed inside the square brackets.
  • (?=.*[a-zA-Z])” indicates that the password must contain at least one letter (uppercase or lowercase).
  • (?=.*\d)” denotes that the string/password contains at least one digit.
  • .{8,12}” tells that the password must be 8 to 12 characters in length.
  • $” denotes the end of the pattern string.

Example

In an HTML file, create a form with an input field to take password input and a submit button. Attach an “onclick” event with the button that will call the function “passwordValidation()” on the form submission to validate the given password:

<form name="form" action="#">
<input type="text" id="txt" placeholder="Enter your Password" autocomplete="off">
<button type="submit" onclick="passwordValidation()">Submit</button>
<p id="msg"></p>
<p id="error"></p>
</form>

Create a div element and mention the rules for the password in the unordered list:

<div id="box">
<ul>
<h5>Follow the given format for Password</h5>
<li>Password must be 8-12 characters</li>
<li>At least one alphabet (uppercase or lowercase)</li>
<li>At least one number present in the password</li>
<li>At least one special character (-,.,@,$,!,%,+,=,<,>,#,?,&)</li>
</ul>
</div>

In the CSS file, create ids “error” and “msg” and set the colors “red” and “green”, respectively. Assign these ids to the <p> tags for showing messages when the password will correct or incorrect:

#error{
color: red;
}#msg{
color: green;
}

Create an id “box” that assigns to the div element to set the font size of the text:

#box{
font-size: small;
}

In JavaScript file, use the following code for validating the password:

functionpasswordValidation()
{
 regexPattern = /^(?=.*[-\#\$\.\%\&\@\!\+\=\\*])(?=.*[a-zA-Z])(?=.*\d).{8,12}$/;
 inputTextVal = document.getElementById("txt");
if(inputTextVal.value.match(regexPattern))
 {
document.getElementById("msg").innerHTML = "Great";
returntrue;
 }
else
 {
document.getElementById("error").innerHTML = "Please follow the proper format";
returnfalse;
 }
}

In the above code snippet:

  • First, define a function “passwordValidation()” which will trigger on the button click.
  • Define a regex pattern for password validation having special characters.
  • Get the reference of the input field using the “getElementById()” method.
  • Then, check the input value whether it matches the pattern or not using the “match()” method.
  • If the password matches the pattern, print the message “Great”, else, print the error message.

Output

Now, for accepting input as a password, use the input type “password”:

<input type="password" id="txt" placeholder="Enter your Password" autocomplete="off">

You can see that the output takes password in the password format:

That’s all about the validation of passwords having special characters.

Conclusion

Use “/^(?=.*[-\#\$\.\%\&\@\!\+\=\<\>\*])(?=.*[a-zA-Z])(?=.*\d).{8,12}$/” for special characters in password validation. The pattern will consider a password that contains at least one alphabet, special character, and a digit of length 8 to 12 characters. This article described the regular expression or pattern for validation of passwords containing special characters.

 

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.