Gilbert Mendoza
Gilbert Mendoza

Reputation: 415

Detect two or more same character in one string

Simple Question. How can I detect if my string contains two same character on input keyup. I prefer to do this in jquery

test >> true
abcd >> false

I've tried

  $('#input-id').on('keyup', function(event) {
    if($('#input-id:contains("t")').length >= 2){
       alert('123');
    }
  });

but this is not working

Upvotes: 0

Views: 71

Answers (2)

Narayan
Narayan

Reputation: 1668

Try this ==>

var str= $('inputId').value;
var hasrepeat = (/([a-zA-Z]).*?\1/).test(str)        
alert(hasrepeat); // return true OR false if true=repeat false= Not repeat

Upvotes: 0

Tamilvanan
Tamilvanan

Reputation: 718

Try this...

var input = $('inputId').value;
var count = (input.match(/t/g) || []).length;  // 't' is a search string
if(count > 1)
 return true;
else
 return false;

Upvotes: 1

Related Questions