Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if all characters in a string are same

Tags:

jquery

I want to know if all characters in a string are same. I am using it for a Password so that i tell the user that your password is very obvious. I have crated this

$(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 achieve the same characters?

like image 525
Fawad Ghafoor Avatar asked Jul 08 '11 02:07

Fawad Ghafoor


2 Answers

/^(.)\1+$/.test(pw) // true when "aaaa", false when "aaab".

Captures the first character using regex, then backreferences it (\1) checking if it's been repeated.

Here is the fiddle that Brad Christie posted in the comments

like image 118
Brad Christie Avatar answered Sep 27 '22 19:09

Brad Christie


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");
    }
    }
like image 43
The Mask Avatar answered Sep 27 '22 20:09

The Mask