1

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.

3
  • 1
    Hi @Addy.Please read through this and see if this helps ->stackoverflow.com/questions/1559751/… Commented Dec 18, 2017 at 12:02
  • I don't understand what you want here? Can you clarify? Commented Dec 18, 2017 at 12:03
  • @Liam actually I want a regex which validates this string contains case as I provide in my Question there all the case I match except the Alphabets part which contains at least one lower and one upper case letter. Commented Dec 18, 2017 at 12:06

3 Answers 3

2

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"/>

Sign up to request clarification or add additional context in comments.

3 Comments

this also not work if the string does not contain an upper or lower char.
@addy it will work based on your regexp /^([-\/@#!*$%^&.'_+={}()]{4})([a-zA-Z]{4})([0-9]{4})$/ i taken this from your question based on your try
sorry buddy also have an issue with alphabets part this also provides a match if the string does not contain an upper or lower case char.
1

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! :)

6 Comments

Good try boy but there I have an issue with a password with a specific count of chars.This works individual like my tried regex but not accomplish my task
So if I understood it correctly: You need to check the first 4 chars, the next 4 and the last 4 (in your situation)?
Do you know about charAt and substring?
Yes, But this not an answer and suggestion for my question.
|
1

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}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.