0

I'm having trouble figuring out why this function works in chrome, but not IE:

function isOkPass(p){
    var categories = 0;
    var anUpperCase = /[A-Z]/;
    var aLowerCase = /[a-z]/; 
    var aNumber = /[0-9]/;
    var aSpecial = /[!|@|#|$|%|^|&|*|(|)|-|_]/;
    var obj = {};
    obj.result = true;

    if(p.length < 8){
        obj.result=false;
        obj.error="Password not long enough!"
        return obj;
    }

    var numUpper = 0;
    var numLower = 0;
    var numNums = 0;
    var numSpecials = 0;
    for(var i=0; i<p.length; i++){
        if(anUpperCase.test(p[i])) {
            numUpper++;
console.dir('UPPPER');
        } else if(aLowerCase.test(p[i])) {
            numLower++;
console.dir('LOWEERR');
        } else if(aNumber.test(p[i])) {
            numNums++;
console.dir('NUMBER');
        } else if(aSpecial.test(p[i])) {
            numSpecials++;
console.dir('special');
        }
    }

    if(numUpper >= 1){
        categories += 1;
    }
    if(numLower >= 1){
        categories += 1;
    }
    if(numNums >= 1){
        categories += 1;
    }
    if(numSpecials >= 1){
        categories += 1;
    }

    if(categories < 3){
        obj.categories= categories;
        obj.result= false;
        obj.error= "Password not complex enough!";
        return obj;
    }

    return obj;
}

The user enters a password in an input box and when they click out of the box the string is validated with this function. In chrome it works fine, but in IE it appears test() is behaving strangely.

I added some debug messages and found that when I enter a number in the input box (and click out of it) IE displays the "LOWER" debug message where as chrome displays the "NUMBER" debug message as expected. What's wrong with my code?

Edit: after you guys pointed out the stupidity of how the string was being evaluated (I think I grabbed a script that was originally supposed to record every type of character) I'm now just doing this:

if(aUpperCase.test(p)) {
    numUpper++;
};
if(aLowerCase.test(p)) {
    numLower++;
};
if(aNumber.test(p)) {
    numNums++;
};
if(aSpecial.test(p)) {
    numSpecials++;
};
red888
  • 27,709
  • 55
  • 204
  • 392
  • 1
    You don't need those `|` characters in `/[!|@|#|$|%|^|&|*|(|)|-|_]/`. – Pointy Nov 03 '14 at 16:18
  • Why do you have alternation operators `|` inside your character class `[]`? –  Nov 03 '14 at 16:18
  • 2
    Also, for what it's worth, "complexity" tests like this are a bad idea. A ten-character all-lower-case password made up of randomly-chosen letters is a strong password, but this test won't allow it. – Pointy Nov 03 '14 at 16:21
  • What are you passing in for `p`? I don't see any regex issues, but maybe you're not passing in the input's text correctly. – Jacob Nov 03 '14 at 16:22
  • @Pointy Very true, Complexity never out ways Length in a brute force attack. Just make them have long passwords and you're secure 99% of the time. – Joe Swindell Nov 03 '14 at 16:22
  • The issue isn't IE it is your regex. – abc123 Nov 03 '14 at 16:23
  • 2
    I'm not getting this logic at all. You're running the regex's on **each individual character**--why? –  Nov 03 '14 at 16:24
  • 2
    You're indexing into a string, which isn't supported in some versions of IE. Try `p.charAt(i)`. Or make sure you're in IE Standars Mode. –  Nov 03 '14 at 16:24
  • It has to match the password policy of our IDM platform (Active Directory). These are the default complexity requirements of AD (minus password history). – red888 Nov 03 '14 at 16:28
  • [This Q and A](http://stackoverflow.com/questions/20586480/javascript-regex-that-requires-one-uppercase-letter-one-lowercase-letter-and-on) may help you do it with a single regex. –  Nov 03 '14 at 16:31
  • p.charAt(i) was what I needed. As squint said I was attempting to index into a string which chrome allowed, but IE didn't. And now to delete this question as per the down vote. – red888 Nov 03 '14 at 16:32
  • Probably more important is to make sure you're in standards mode. IE 8 and up should allow string indexing, but not in quirks mode. Do you have a doctype for your HTML document? –  Nov 03 '14 at 16:32
  • I did not have a doctype! Thanks for that I usually make sure of this, but forgot this time. – red888 Nov 03 '14 at 16:34
  • 2
    Now, you don't need `p.charAt(i)`. You **don't need to regexp your string one character at a time**. Think about it. Running the `test` against the **whole string** will succeed if there's a least one character of the given type, which is what you want. –  Nov 03 '14 at 16:35
  • @torazaburo is right. While the `.charAt()` or the doctype solves the immediate problem, your overall approach can be improved. But +1 because it's a good, detailed question. –  Nov 03 '14 at 16:39
  • @torazaburo thanks, must admit I just copied most of this snippet and made sure it worked without checking the sanity of the thing. – red888 Nov 03 '14 at 16:39

0 Answers0