Field CPF, CNPJ and Other Document - jQuery validation [closed]

-2

I have a field where the CPF, CNPJ and Other Document can be written. The field is unmasked, while the "Other Document" can be any number or letter.

How can I make these validations in jQuery ? Make sure the user typed in without a mask or a mask and / or a document.

    
asked by anonymous 23.12.2015 / 14:02

3 answers

1

You will only be able to do this when the courses leave the field, before that you can not predict what will be inserted: CPF, CNPJ or Other Document.

I would do so: In%% of - field output - would take text content, filter only numbers and if you have 11 digits would format as CPF, if you have 14 as CNPJ, otherwise, you would not format.

    
23.12.2015 / 14:10
2

It seems from your description that any string that contains numbers or letters is valid. In this case a simple match is enough:

var valido = this.value.match(/^[\d\w]+$/);

Example:

var input = document.querySelector('input');
var res = document.getElementById('res');
input.addEventListener('keyup', function() {
    var valido = this.value.match(/^[\d\w]+$/);
    res.innerHTML = valido ? 'valido' : 'invalido';
});
<input type="text" placeholder="escreve algo aqui...">
<div id="res"></div>

jsFiddle: link

    
23.12.2015 / 14:14
1

There is a well-known jQuery plugin called jQuery Validation Plugin which facilitates a lot of validation processes using jQuery. You can take a look at their documentation to learn how to use here .

    
23.12.2015 / 14:28