Reputation: 2372
we need to check if the security password is documented correctly for an Internal Web CRM.
Is there a way to validate a given security password like a "$$$$Addy1234"
?
This password string contains 4 Special Char, 4 Alphabets and 4 Number.And Alphabets part should contain one Upper and one Lower Case Character.
The regex I tried that work with all the case but in the Alphabets, I need to get at least one Upper and one Lower Case Character.
I tried following but can't get a solution:
$("#btn_submit").click(function () {
if ($("#txt_pasword").filter(function () {
return this.value.match(/^([-\/@#!*$%^&.'_+={}()]{4})([a-zA-Z]{4})([0-9]{4})$/);
})) {
$("#txt_pasword").parent().child("span").text("pass");
} else {
$("#txt_pasword").parent().child("span").text("fail");
}
});
Please provide an idea how should I do this?
Thank you in advance.
Upvotes: 1
Views: 70
Reputation: 2372
Thanx to all people who provide me solution for this problem mainly @kalaiselvan-a and @ronnie-oosting.
But from the idea @kalaiselvan-a give than approximately correct with few of issue but this one helps me to get the solution accordingly.
Used Regex is :
/^([-\/@#!*$%^&.'_+={}()]{4})((?=.*[a-z])(?=.*[A-Z])[a-zA-Z]{4})([0-9]{4})$/
1st Capturing Group ([-\/@#!*$%^&.'_+={}()]{4})
Matches exactly 4 times character in the list @#!*$%^&.'_+={}()
(case sensitive)
2nd Capturing Group ((?=.*[a-z])(?=.*[A-Z])[a-zA-Z]{4})
Matches exactly 4 times character in the list [a-z]
and [A-Z]
with one of each
3rd Capturing Group ([0-9]{4})
Matches exactly 4 times a character present in the list [0-9]{4}
Upvotes: 1
Reputation: 1252
Would this help you?
Code:
$(document).ready(function() {
var str = "$$$$Addy1234";
var res = str.substring(0,3);
var res2 = str.substring(4,7);
var res3 = str.substring(8, 11);
var check = 0;
// check special chars
if (res.match(/([!,%,&,@,#,$,^,*,?,_,~])/))
{
check += 1;
}
// check upper/lower cases
if (res2.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))
{
check += 1;
}
// check numbers
if (res3.match(/([0-9])/))
{
check += 1;
}
if( check < 3)
{
// return false
}
if (check === 3)
{
// return true
}
});
You can make different checks:
// If password contains both lower and uppercase characters, increase strength value.
if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) strength += 1
// If it has numbers and characters, increase strength value.
if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)) strength += 1
// If it has one special character, increase strength value.
if (password.match(/([!,%,&,@,#,$,^,*,?,_,~])/)) strength += 1
// If it has two special characters, increase strength value.
if (password.match(/(.*[!,%,&,@,#,$,^,*,?,_,~].*[!,%,&,@,#,$,^,*,?,_,~])/)) strength += 1
You can check your own regex based on what given.
Using substring
you can check specific length with a start and end.
Will this do it for you? Let me know! :)
Upvotes: 1
Reputation: 2134
In the below code you can use your own logic for display error message but here I used alert
$(function(){
$("#btn_submit").click(function () {
var mat_str=/^([-\/@#!*$%^&.'_+={}()]{4})((?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{4,})([0-9]{4})$/;
var pass=$("#txt_pasword").val();
if(pass!=null && pass!=undefined){
if(mat_str.test(pass)){
alert("pass");
}else{
alert("fails");
}
}else{
alert("Please Enter password");
}
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<input type="password" id="txt_pasword">
<span></span>
</div>
<input type="button" id="btn_submit" value="submit"/>
Upvotes: 2