2

Hello every one I am new to regex field I am just looking forward to create a regex which is not having digits repeating more than five times as well as it should start from only 7,8,9 digits.

I have created regex in this manner which is giving me my first condition such as it is starting from only 7,8,9. but there is problem I can not understand how to limit the repeatation of any single digits to only five times. Please help.

my regex   ^[789]\d{9}$
  • 3
    I don't know about you, but a friend of mine has a phone number with the digit 7 appearing five times. Should he not be allowed to use your system? – Niet the Dark Absol Jan 10 '18 at 11:14
  • the contact number having any digit repeating for 5 times is allowed to use in my system but not more than 5. Hence I want to validate in that manner. – Gaurav Chauhan Jan 10 '18 at 11:16
  • 1
    What kind of arbitrary restriction is that? Are you in charge of what phone numbers people get assigned as well? – CBroe Jan 10 '18 at 11:26

2 Answers2

1

but there is problem I can not understand how to limit the repeatation of any single digits to only five times

You can check if a digits is repeated N number of times (using this answer )

var checkIfDigitRepeatedNTimes = function(input, N) 
{
  var regex = new RegExp("(\\d)\\1{" + N + "}", "g");
  //console.log( regex );
  return !!input.match(regex);
}

Demo

var checkIfDigitRepeatedNTimes = function(input, N) 
{
  var regex = new RegExp("(\\d)\\1{" + N + "}", "g");
  //console.log( regex );
  return !!input.match(regex);
}

console.log( checkIfDigitRepeatedNTimes( "454324154", 5 ) );

console.log( checkIfDigitRepeatedNTimes( "789666666", 5 ) );

Edit

Use checkIfDigitRepeatedNTimes method along with your regex as

var regex = /^[789]\d{9}$/;
var input = "9851542144";
var isValid = !!input.match( regex ) && checkIfDigitRepeatedNTimes( input, 5 );

Edit 2

If the requirement is to ensure that repetition is not necessarily consecutive then modify the checkIfDigitRepeatedNTimes as

var checkIfDigitRepeatedNTimes = function(input, N) 
{
  input = input.split("").sort().join(""); //sorting the input 
  var regex = new RegExp("(\\d)\\1{" + N + "}", "g");
  //console.log( regex );
  return !!input.match(regex);
}
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • @Toto Didn't realize that OP hasn't asked for consecutive repetition check. I have updated the answer. – gurvinder372 Jan 10 '18 at 11:31
  • 1
    I think `sort` is a good option. I can't see a single reges that could do the job. – Toto Jan 10 '18 at 11:38
  • @gurvinder372 it is correct method I was looking for actually I have used both function separately and merged their result into one result to get my output. Thanks a lot !! – Gaurav Chauhan Jan 10 '18 at 12:29
0

Since this question is tagged php, I'll provide a demo in php, but the patterns will work with javascript as well (no digit-sorting necessary). My second pattern in the method allows for non-consecutive repetitions of a digit.

Code: (Demo)

$tests=['1234567890'=>'bad start',
        '81234567890'=>'too long',
        '1211211199'=>'bad start, six repeats',
        '1111112233'=>'bad start, six repeats',
        '2111111222'=>'bad start, six repeats',
        '8221111111'=>'seven repeats',
        '1211121212'=>'six repeats',
        '9999999999'=>'ten repeats',
        '8901234567'=>'valid',
        '7677677677'=>'seven repeats',
        '8888833333'=>'valid',
        '9112345789'=>'valid',
        '9444449555'=>'valid',
        '6666655544'=>'bad start'];

foreach($tests as $test=>$prediction){
    echo "$test ";
    if(preg_match('/^[7-9]\d{9}$/',$test) && !preg_match('/(\d)(?=(?:\d*\1){5})/',$test)){
        echo "pass";
    }else{
        echo "fail";
    }
    echo " \t($prediction)\n";
}

Output:

1234567890 fail     (bad start)
81234567890 fail    (too long)
1211211199 fail     (bad start, six repeats)
1111112233 fail     (bad start, six repeats)
2111111222 fail     (bad start, six repeats)
8221111111 fail     (seven repeats)
1211121212 fail     (six repeats)
9999999999 fail     (ten repeats)
8901234567 pass     (valid)
7677677677 fail     (seven repeats)
8888833333 pass     (valid)
9112345789 pass     (valid)
9444449555 pass     (valid)
6666655544 fail     (bad start)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • @Toto here is a `sort`-less method. I tried many, many patterns (probably a couple hours) but couldn't manage to write a solution as a single pattern. I'd love to see it if a single regex solution is possible. – mickmackusa Jan 11 '18 at 14:12