Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

556
Visualizações
jQuery Validation - password MUST contain uppercase and number

So in my registration form I have this field:

 <div class="form-group">
    <label for="RegisterModel_Password">Password</label>
    <input type="password" id="RegisterModel_Password" 
           name="RegisterModel.Password" class="form-control" 
           required="required" minlength="8"/>
</div>

As you see, I'm using jQuery validation attributes to ensure that the password includes at least 8 characters. So, I want to check if password contains uppercase and number, if not, field is not valid. I downloaded additional method for jQuery Validation plugin named "pattern" and added her in head tag.

I tried to do this as follows but it didn't worked.

$("#formRegister").validate({
    rules: {
        RegisterModel_Password: {
            pattern: /^[a-zA-Z][0-9]/
        }
    }
});

I assume that the pattern is wrong, but I'm not sure whether the use is correct. Thank you for your help.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Chains of regular expressions are too hard for me ( I have never tried to learn them lol ). So here is my solution:

jQuery.validator.addMethod("passwordCheck",
        function(value, element, param) {
            if (this.optional(element)) {
                return true;
            } else if (!/[A-Z]/.test(value)) {
                return false;
            } else if (!/[a-z]/.test(value)) {
                return false;
            } else if (!/[0-9]/.test(value)) {
                return false;
            }

            return true;
        },
        "error msg here");

And simply I use it like a attribute:

<input type="password" id="RegisterModel_Password" 
name="RegisterModel.Password" 
class="form-control" 
required="required" minlength="8" 
passwordCheck="passwordCheck"/>

Thanks for your answers.

about 4 years ago · Santiago Trujillo Relatório

0

You can add your custom validation using $.validator.addMethod() like:

$.validator.addMethod("validation_name", function(value) {
    // at least 1 number and at least 1 character
    [^\w\d]*(([0-9]+.*[A-Za-z]+.*)|[A-Za-z]+.*([0-9]+.*))
});
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda