Asked  7 Months ago    Answers:  5   Viewed   36 times

I'm creating a regexp for password validation to be used in a Java application as a configuration parameter.

The regexp is:

^.*(?=.{8,})(?=..*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$

The password policy is:

  • At least 8 chars

  • Contains at least one digit

  • Contains at least one lower alpha char and one upper alpha char

  • Contains at least one char within a set of special chars (@#%$^ etc.)

  • Does not contain space, tab, etc.

I’m missing just point 5. I'm not able to have the regexp check for space, tab, carriage return, etc.

Could anyone help me?

 Answers

54

Try this:

^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=S+$).{8,}$

Explanation:

^                 # start-of-string
(?=.*[0-9])       # a digit must occur at least once
(?=.*[a-z])       # a lower case letter must occur at least once
(?=.*[A-Z])       # an upper case letter must occur at least once
(?=.*[@#$%^&+=])  # a special character must occur at least once
(?=S+$)          # no whitespace allowed in the entire string
.{8,}             # anything, at least eight places though
$                 # end-of-string

It's easy to add, modify or remove individual rules, since every rule is an independent "module".

The (?=.*[xyz]) construct eats the entire string (.*) and backtracks to the first occurrence where [xyz] can match. It succeeds if [xyz] is found, it fails otherwise.

The alternative would be using a reluctant qualifier: (?=.*?[xyz]). For a password check, this will hardly make any difference, for much longer strings it could be the more efficient variant.

The most efficient variant (but hardest to read and maintain, therefore the most error-prone) would be (?=[^xyz]*[xyz]), of course. For a regex of this length and for this purpose, I would dis-recommend doing it that way, as it has no real benefits.

Tuesday, June 1, 2021
 
muaaz
answered 7 Months ago
43

Continue calling re.exec(s) in a loop to obtain all the matches:

var re = /s*([^[:]+):"([^"]+)"/g;
var s = '[description:"aoeu" uuid:"123sth"]';
var m;

do {
    m = re.exec(s);
    if (m) {
        console.log(m[1], m[2]);
    }
} while (m);

Try it with this JSFiddle: https://jsfiddle.net/7yS2V/

Tuesday, June 1, 2021
 
barden
answered 7 Months ago
85

You may use

const reg = '(https?://)?([\da-z.-]+)\.([a-z.]{2,6})[/\w .-]*/?';

The ^ (at the start) and $ (at the end) will be added by Angular2 automatically (note that you are in charge of properly grouping the pattern in that case, though in this case it is not required).

The most important part here is that you need to double the escaping backslash in the string literal to define a literal backslash that escapes special regex metacharacters.

Also, you do not need to escape / in a regex constructor notation.

Also, you have ([/w .-]*)* that is a very poor pattern: it is the same as [/\w .-]*, so remove the quantified grouping here.

Wednesday, August 25, 2021
 
DilbertDave
answered 4 Months ago
69

UPDATED:

Sorry, the problem is with your regex, i missed that, change to this its fully working:

  var ck_password =  /^[A-Za-z0-9-]/;

            if(!ck_password.test(stringf))
            {

                alert("Family name can only contain alphanumeric characters and hypehns(-)")

            }

Console in chrome, go to the OPTIONS in the right top coner, select TOOLS, then DEVELOPER TOOLS.

Saturday, August 28, 2021
 
itowlson
answered 3 Months ago
78

Correct way to iterate the collection will be: (full example)

function confirm_update() {
    var arrCheckboxes = document.deleteFiles.elements["files[]"];
    var checkCount = 0;
    for (var i = 0; i < arrCheckboxes.length; i++) {
        checkCount += (arrCheckboxes[i].checked) ? 1 : 0;
    }

    if (checkCount > 0){
        return confirm("Are you sure you want to proceed deleting the selected   files?");
    } else {
        alert("You do not have any selected files to delete.");
        return false;
    }
}
body {
    margin: 30px;
}
<form name="deleteFiles" action="" method="post" onsubmit="return confirm_update();">
    
    <input type='checkbox' name='files[]' id='1' value='1' /> file 1<br />
    <input type='checkbox' name='files[]' id='2' value='2' /> file 2<br />
    <input type='checkbox' name='files[]' id='3' value='3' /> file 3<br />
    <input type="submit" value="Submit" name="submit" />
    
</form>
Saturday, August 28, 2021
 
Sibtain
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share