Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Regex with limit number and non repetitive

Eric80
Giga Contributor

Hi, there. 

 

Could you help me with this RegEx, please?

 

I'm trying to  make a field with limited numbers e non repetitive. I did a regex like this:

     if (!/^[0-8]\1{1,}$/.test(newValue)) {
         g_form.clearValue('value_field');
         g_form.showFieldMsg('value_field', 'msg', 'error');

     }
}

but isn't working, its don't let me type any kind of character in it. 

 

4 REPLIES 4

sachin_namjoshi
Kilo Patron
Kilo Patron

You can use below regex for 8 digit non repeating numbers.

 

^(\d)(?!\1{7})\d{7}$

 

Regards,

Sachin

_ChrisHelming
Tera Guru

Can you elaborate? You want the user to be able to enter a digit 1-8 but without repeating the same number? Or without the number being used consecutively?

^(?:([1-8])(?!.*\1)){8}$

/*
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match the regular expression below «(?:([1-8])(?!.*\1))*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   Match the regular expression below and capture its match into backreference number 1 «([1-8])»
      Match a single character in the range between "1" and "8" «[1-8]»
   Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!.*\1)»
      Match any single character that is not a line break character «.*»
         Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
      Match the same text as most recently matched by capturing group number 1 «\1»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
*/

/* 
12345678 - Pass
12134567 - Fail (1 is repeated)
12234567 - Fail (2 is repeated next to itself)
*/

^(?:([1-8])(?!\1)){8}$

/*
12345678 - Pass
12134567 - Pass (1 *isn't* next to itself)
12234567 - Fail (2 is repeated next to itself)
*/

Remove or modify the {8} for how many chars long you want the entered value to be.

Modify the [0-8] with what digits you want to be allowed (or replace the entire [0-8] with \d if you want to allow all digits).

Eric80
Giga Contributor

The field must not accept the sequency: 11111111, 22222222, 33333333 and so on, for example. But, must accept digits numbers as: 24446795, 33322458 even with 2 or 3 more repeated digits in it. 

Hi Eric,
Did you find a solution for this? If yes, could you pls post it here. I have the same exact requriement.
Thank you!
Sumana