Password Validation using REGEX Regular Expression in Javascript

Password Validation using REGEX Regular Expression in Javascript mytechlearns
JavaScriptLeave a Comment on Password Validation using REGEX Regular Expression in Javascript

Password Validation using REGEX Regular Expression in Javascript

Password Validation using REGEX Regular Expression in Javascript

Every software Password is very sensitive and very much important part that can authenticate user. For this reason, software developer use different types of validaion use. Here we discuss four different types of Password validation. Now first of this:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>JavaScript form validation - Password Validation</title>
</head>
<body onload='document.form1.text1.focus()'>
	<div class="mail">
	<h2>Input Password and Submit [7 to 20 characters which contain only characters, numeric digits, underscore and first character must be a letter]</h2>
	<form name="form1" action="#">
		<input type='text' name='text1'/>
		<input type="submit" name="submit" value="Submit" onclick="CheckPassword(document.form1.text1)"/>
	</form>
	</div>
	<script type="text/javascript">

		function CheckPassword(inputtxt) 
		{ 
			var passw=  /^[A-Za-z]\w{7,21}$/;
			if(inputtxt.value.match(passw)) 
			{ 
				alert('Correct...!')
				return true;
			}
			else
			{ 
				alert('Wrong...!')
				return false;
			}
		}


	</script>
</body>
</html>

Example 1: 7 to 20 characters which contain only characters, numeric digits, underscore and first character must be a letter

Regular Expression : “/^[A-Za-z]\w{7,21}$/”

Then the second example:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>JavaScript form validation - Password Validation</title>
</head>
<body onload='document.form1.text1.focus()'>
	<div class="mail">
	<h2>Input Password and Submit [6 to 20 characters which contain at least one numeric digit, one uppercase and one lowercase letter]</h2>
	<form name="form1" action="#">
		<input type='text' name='text1'/>
		<input type="submit" name="submit" value="Submit" onclick="CheckPassword(document.form1.text1)"/>
	</form>
	</div>
	<script type="text/javascript">

		function CheckPassword(inputtxt) 
		{ 
			var passw=  /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/;
			if(inputtxt.value.match(passw)) 
			{ 
				alert('Correct...!')
				return true;
			}
			else
			{ 
				alert('Wrong...!')
				return false;
			}
		}


	</script>
</body>
</html>

Example 2: 6 to 20 characters which contain at least one numeric digit, one uppercase and one lowercase letter

Regular Expression : “/^(?=.\d)(?=.[a-z])(?=.*[A-Z]).{6,20}$/”

Then the third example:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>JavaScript form validation - Password Validation</title>
</head>
<body onload='document.form1.text1.focus()'>
	<div class="mail">
	<h2>Input Password and Submit [7 to 15 characters which contain at least one numeric digit and a special character]</h2>
	<form name="form1" action="#">
		<input type='text' name='text1'/>
		<input type="submit" name="submit" value="Submit" onclick="CheckPassword(document.form1.text1)"/>
	</form>
	</div>
	<script type="text/javascript">

		function CheckPassword(inputtxt) 
		{ 
			var passw=  /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{7,15}$/;
			if(inputtxt.value.match(passw)) 
			{ 
				alert('Correct...!')
				return true;
			}
			else
			{ 
				alert('Wrong...!')
				return false;
			}
		}


	</script>
</body>
</html>

Example 3: 7 to 15 characters which contain at least one numeric digit and a special character

Regular Expression : “/^(?=.[0-9])(?=.[!@#$%^&])[a-zA-Z0-9!@#$%^&]{7,15}$/”

Then the fourth example:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>JavaScript form validation - Password Validation</title>
</head>
<body onload='document.form1.text1.focus()'>
	<div class="mail">
	<h2>Input Password and Submit [8 to 15 characters which contain at least one lowercase letter, one uppercase letter, one numeric digit, and one special character]</h2>
	<form name="form1" action="#">
		<input type='text' name='text1'/>
		<input type="submit" name="submit" value="Submit" onclick="CheckPassword(document.form1.text1)"/>
	</form>
	</div>
	<script type="text/javascript">

		function CheckPassword(inputtxt) 
		{ 
			var passw=  /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,15}$/;
			if(inputtxt.value.match(passw)) 
			{ 
				alert('Correct...!')
				return true;
			}
			else
			{ 
				alert('Wrong...!')
				return false;
			}
		}


	</script>
</body>
</html>

Example 2: 8 to 15 characters which contain at least one lowercase letter, one uppercase letter, one numeric digit, and one special character

Regular Expression : “/^(?=.\d)(?=.[a-z])(?=.[A-Z])(?=.[^a-zA-Z0-9])(?!.*\s).{8,15}$/”

That’s all for today. Happy Coding…

Source Code: Click Here

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top