How to determine if all characters in a string are the same

I want to know if all characters in a string are the same. I use it for a password so I tell the user that your password is very obvious. I broke it

$(function(){
    $('#text_box').keypress(function(){
        var pass = $("#text_box").val();
        if(pass.length<7)
            $("#text_box_span").html('password must be atleast 6 characters');
        else
            $("#text_box_span").html('Good Password');
    });
});

How can I get the same characters?

+5
source share
4 answers
/^(.)\1+$/.test(pw) // true when "aaaa", false when "aaab".

Capturing the first character with a regular expression, then backlink it ( \1), checking to see if it repeats.

Here's a scenario that Brad Christie published comments

+18
source

I wrote in pure javascript:

 var pass = "112345"; 
    var obvious = false; 

    if(pass.length < 7) { 
       alert("password must be atleast 6 characters");
    } else { 

    for(tmp = pass.split(''),x = 0,len = tmp.length; x < len; x++) {
        if(tmp[x] == tmp[x + 1]) {
           obvious = true; 
        }
    }

    if(obvious) { 
       alert("your password is very obvious.");
    } else { 
      alert("Good Password");
    }
    }
+1
source

: http://jsfiddle.net/mazzzzz/SVet6/

function SingleCharacterString (str)
{
    var Fletter = str.substr(0, 1);
    return (str.replace(new RegExp(Fletter, 'g'), "").length == 0); //Remove all letters that are the first letters, if they are all the same, no letters will remain
}

:

$(function(){
    $('#text_box').keypress(function(){
        var pass = $("#text_box").val();
        if(pass.length<7)
            $("#text_box_span").html('password must be atleast 6 characters');
        else if (SingleCharacterString(pass))
            $("#text_box_span").html('Very Obvious.');
        else
            $("#text_box_span").html('Good Password');
    });
});
+1

. , "Z"?

function same(str,char){
    var i = str.length;
    while (i--) {
        if (str[i]!==char){
            return false;
        }
    }
    return true;
}
// same('zzza','z'); returns false
0

All Articles