How to block random characters in Javascript?

Asked

Viewed 140 times

-1

I have a form, and I’d like to remove the random characters. Type:

aaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbb
ccccccccccccccccccccccc
20323092093023029302
weweiowoeiow
o392039209320939

Description: Not just these, I would like to make the form a little secure. Prevent the user from creating things like this. The script below makes an Random "randomness" wanted to use this to prevent this event....

function generateRandomNumber(){
    "use strict";

    // se o browser tiver suporte à getRandomValues()
    if (Uint32Array && window.crypto && window.crypto.getRandomValues) { 
        var numbers = new Uint32Array(1);
        window.crypto.getRandomValues(numbers);
        return numbers[0] * Math.pow(2,-32);

    // caso não tenha, é utilizado Math.random
    } else {
        return Math.random();
    }
}

function shuffle(string) {
    "use strict";
    var parts = string.split('');

    for (var i = parts.length; i > 0;) {
        var random = parseInt(generateRandomNumber() * i); // aqui é chamada a função que gera o número aleatório
        var temp = parts[--i];
        parts[i] = parts[random];
        parts[random] = temp;
    }

    return parts.join('');
}
  • 1

    It was not clear what he wanted to do and much less because the characters cited were considered "random". Could you elaborate a [mcve] for your problem?

  • Could define what random characters are?

  • Would that be "aaaaaaaaaaaaaaaaaaaaaaaaaaa" "sdskjksdkjsjdk" "dksjkjjk23jk23kj" Rs...

  • I did not understand. You want to prevent "random numbers" in your form and has a function that "generates" such numbers? What is the function for?

  • Rs.... is an example... I want to prevent "random numbers"... And I used this example, wanted to understand how it works... to prevent this.

  • If you need to validate user-provided data, take a look at regular expressions and mask application, just to get started. There is vast material on this content on the internet (handle user data - user input).

  • okay ;D you are beast and whenever I can include all the names in my 'programs'

  • There is no way to predict what a user can type to circumvent an input, even when there are no phone masks I fill "Semtelefone", "Sememail".

  • You’re right ;D face

  • People, with the answer! Can I create a regular expression with certain patterns and block or prevent it from being done in the input? The example below, the friend mentioned that it is possible to create a dictionary and prevent these words! Now, I think you could use regular expressions to make a pattern and so prevent or block via input? Example, the pattern 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' .... the expression could be 'a' (sequence). If 'a-z has a sequence without space' = lock, prevent.

  • If a-z has randomness... Example, 'askdjskdksjdkjskdjsdkjweowieoweiwoieoweowie' = block, prevent.

Show 6 more comments

1 answer

0


Dude I made something that can help you, the idea is you put inside the words array that are banned and then you can control what the user can not send

var lista_palavras_banidas = [
"aaaaaaa", "jsdkjfids", "jijaswwww", "bebbbebeb"
];

function verifica(){
  var b = false;
  var a = document.getElementById("inputA").value;
  for (let index = 0; index < lista_palavras_banidas.length; index++) {
    if(lista_palavras_banidas[index] === a ){
      b = true;
      break;
    };

  }
  console.log(b);
  if(b){
    inputInvalid();
  }
}

function inputInvalid(){
document.getElementById("inputA").style.border = "2px solid red";
}
<html>
  <body>
  
  <form>
    <input id="inputA" type="text" onkeyup="javascript:verifica()" />
  </form>
 
  </body>
</html>

Browser other questions tagged

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