Check string? [closed]

1

In event keyup of a input I want to check the following cases:

  • If string has letter and number "abc123" ;
  • If string is uppercase and lowercase "aBc" ;
  • If string has numeric alpha character "a#B1 c" ;

How can I see if string has these caracteres ?

    
asked by anonymous 28.12.2015 / 21:09

2 answers

5

I made a version that gradually checks what you specified in the question.

    $('#test').keyup(function(){
      var value = $(this).val();
      var letras,
          numeros,
          letrasMaiusculas,
          especial;

      if(/[a-z]/gm.test(value)){
        letras = " Letras minúsculas";
      }else{
        letras = "";
      }
      if(/[0-9]/gm.test(value)){
        numeros = " Números ";
      }else{
        numeros = "";
      }
      if(/[A-Z]/gm.test(value)){
        letrasMaiusculas = " letras maiúsculas ";
      }else{
        letrasMaiusculas = "";
      }
      if(/[!@#$%*()_+^&{}}:;?.]/gm.test(value)){
        especial = " Caracteres especiais não alfaNuméricos ";
      }else{
        especial = "";
      }

      $('p').html("O string possui: " + letras + "|" + numeros + "|" + letrasMaiusculas + "|" + especial)
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><inputtype="text" id="test">
<br>
<p></p>

But it is important to know that the password still has some more specifications, I did only what I had in the question, but for a complete model I imagine one of Bacco's examples will do.

    
28.12.2015 / 21:34
2

Example:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <script src="http://xregexp.com/v/3.0.0/xregexp-all-min.js"></script><script>varregex=XRegExp('([\p{Lu}]+[\p{Ll}]+|[\p{Ll}]+[\p{Lu}]+)|[0-9]+[\p{Lu}\p{Ll}]+|[\p{Lu}\p{Ll}]+[0-9]+|([^\p{L}0-9])+');console.log("Ola1: "+regex.test("Ola1"));
        console.log("olá: "+regex.test("olá"));

    </script>
    <title>Hey</title>
</head>
<body>

</body>
</html>

Result: (see console)

Ola1: true
olá: false 

Explanation:

Step 1: Include this script ( link ) no html

<script src="http://xregexp.com/v/3.0.0/xregexp-all-min.js"></script>

Step2:Declaretheregextouse

varregex=XRegExp('([\p{Lu}]+[\p{Ll}]+|[\p{Ll}]+[\p{Lu}]+)|[0-9]+[\p{Lu}\p{Ll}]+|[\p{Lu}\p{Ll}]+[0-9]+|([^\p{L}0-9])+');

Step3:Use

varresultado=regex.test("string a testar");

If the result variable is true then the string has any of the cases. Otherwise, it does not.

Only the external script is needed because regex in javascript can not recognize non-English letters (á, Á, ç, é, í, etc.)

If you do not need to recognize these letters just use:

var regex = /(([a-z]+[A-Z]+|[A-Z]+[a-z]+)|([0-9]+[A-Za-z]+)|([a-zA-Z]+[0-9])+|([\W]))/;
var resultado = regex.test("string a testar");
    
29.12.2015 / 03:13