How to detect a certain sequence of numbers in javascript?

Asked

Viewed 1,067 times

0

In the address registration field, I have the zip code. In this cep field, I need to make some validations:

1 - Do not allow the user to type a sequence with two alternating numbers:

EX 1: "12121212"
EX 2: "12312312"

2 - Do not allow the user to type a sequence of a given number:

EX: "111111111"

How could I do that? I couldn’t make a regex that would handle both situations.

  • It will always be sequence of 2 ?

  • can be a sequence of 3 too, is the most I will block.

  • For the second example this case would enter as wrong ? "1102039020392" in relation to "11" ...

  • I don’t want to allow strains like this: 111111111 222222 333333

  • https://viacep.com.br/exemplo/javascript/

  • It worked the answer I put to you @durtto ?

  • Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).

Show 2 more comments

2 answers

1

I first created a function to check for duplicates in the string, which works as follows: check the string passed if the numbers contain the repetition ( using the previous reference ) for the 2 number repetition case.

Then I created one to check repeated content, where I used a reduce in the content; I took the first position and checked if all others are equal to it, if yes, returns true...

In the end I check the negation of existing duplicates and there are all repeated ones so it is a valid string.

var str1 = "121212122";
var str2 = "111111111";
var str3 = "123456789";

var is_repetidos = function(str) {
  return /([0-9])\1+/g.test(str);
}

var todos_iguais = function(str) {

  var conteudo_separado = str.split("");

  return conteudo_separado.reduce(function(a, b) {
    return a == b;
  }, str[0]);

}

var is_valid = function(str) {
  return !(is_repetidos(str) || todos_iguais(str));
}

var dados = [str1, str2, str3];

for (var i = 0; i < dados.length; i++) {
  console.log(dados[i] + " = " + is_valid(dados[i]))
}

1


You can validate repetitions with the following expression: /([0-9]{2,3})\1/g

It will evaluate whether there are groups of 2 or 3 digits repeated, ie : 1212 or 123123

Example:

var cep = document.querySelector("input[name=cep]");


function fazRegex(){
	var reg = /([0-9]{2,3})\1/g;
  
 
  if(reg.test(cep.value))
  	document.querySelector("#resultado").innerHTML = "Errado";
  else
  	document.querySelector("#resultado").innerHTML = "Certo";
}

cep.addEventListener("keyup", fazRegex);
<input type="text" name="cep" onkeyup="fazRegex()"/>
<div id="resultado"></div>

See also on jsFindle

If you want to test the expression more deeply I strongly advise you to visit the site:

regular Expressions 101

  • Did not address the issue of repetition.

  • I edited the answer, see if it goes according to your requirements.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.