Do a string check?

Asked

Viewed 4,198 times

1

At the event keyup of a input I want to verify the following cases:

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

How can I look to see if the string has these caracteres?

  • 1

    Do you just want to know if you have it, or do you want to do something with the data if you have it? The question is a little vague.

  • Only if there is, then depending on whether or not I will do actions on other objects, but not on that data.

  • 1

    I get it. Here you may already have something that helps: http://answall.com/search?q=jquery+regex . I remember seeing something good without Regex too, to validate passwords, already ready on the site. If I find warning. Remembering that, if to use the data on some server-side system, you have to check on the server side as well.

  • 3

    Some similar things, see if any fit: Mount Regex to validate password and Password check.

2 answers

5


I made a version that checks gradually 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>
<input type="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 that one of the examples of Bacco serves you.

  • THANK YOU for giving an example working... so we can study! and UNDERSTAND!

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>
        var regex = 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 (http://xregexp.com/v/3.0.0/xregexp-all-min.js) in html

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

Step 2: Declare regex to be used

var regex = 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])+');

Step 3: Utilise

var resultado = regex.test("string a testar");

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

You only need the external script because regex in javascript cannot recognize non-English letters (á, Á, ç, é, í, etc.)

If you don’t 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");

Browser other questions tagged

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