How to validate jQuery Validator mobile and landline number?

Asked

Viewed 25,097 times

11

I need a method of validation of landline and mobile phone with the jQuery Validator plug-in.

The differentiation of numbers from fixed to mobile is important to validate the sending of SMS.

  • I do not know why they gave 1 negative point to the post. I did it with the intention of helping. And the person who lowered the grade nor said why.

  • 3

    I also don’t know why the downvote (and the vote to close), but wouldn’t it be the case to post this as a question? Quoting the stack overflow blog "is ok to answer your own question as long as you pretend to be participating in Jeopardy" :P Ask a question like "how to validate landline and mobile phone?" and answer your own question instead of a single post where there is no question at all.

  • 3

    @Curious Leandrocurious turn your answer into question + answer, leaving the question instead of the question and the answer instead of the answer, and then everything will be ok. You should follow the question-answer pattern even if you are doing both yourself. Got it?

  • Right! Thanks for the tips.

  • 1

    Good tip from Math and mgibsonbr. I advise you next time to do the same, which is something we appreciate, to ask a real question face and give the answer to it. People are voting to close the question. Because the way it is, maybe only you can answer it because you know the answer. His intention is very good, just lacked to keep the question clearer. Maybe someone else will answer too and maybe give you a better way to do the same.

  • This jquery plugin is exactly what you’re looking for. It is an extension of jQuery Validator http://rods.la/validator/

Show 1 more comment

3 answers

7

after a few hours analyzing everything we have on Brazilian phones, I created a function to validate the phones, including:

  • Validation of repeated numbers
  • Character quantity validation (if 10 or 11 numbers, other than characters that do not count)
  • Cellular validation (9th digit) if the number has 11 digits
  • Validation of Ddds (valid Ddds seed) - Thanks @renatoargh
  • Validate if the number is the same phone or the radio type (if the number starts between 2 and 5, or 7) - Valid only after 2017, if ANATEL does everything right

ANATEL hasn’t implemented everything yet, but when someone sees this and is already in 2017 or later (look I’m just talking to someone from the future haha) just delete the line that checks the year, which is referenced in the code.

see an example in operation:Jsfiddle

Also follow Gist for anyone who wants to help improve the code or report errors: Gist

Thanks also to @Leandro Curious, because I based my code on your.

  • Very good function! Grateful, man of the past! Hahaha

6


From experience in web development it is very common to see that several forms that validate landline or mobile phone only contemplate the minimum and maximum number of characters, but in fact there is no more consistent verification to differentiate one from the other. It is often necessary to send an SMS and the mobile number is also poorly validated.

For developers using the plug-in jQuery Validator

Below are two methods for making the database more integrated and helping with validation.

//Celular
jQuery.validator.addMethod('celular', function (value, element) {
    value = value.replace("(","");
    value = value.replace(")", "");
    value = value.replace("-", "");
    value = value.replace(" ", "").trim();
    if (value == '0000000000') {
        return (this.optional(element) || false);
    } else if (value == '00000000000') {
        return (this.optional(element) || false);
    } 
    if (["00", "01", "02", "03", , "04", , "05", , "06", , "07", , "08", "09", "10"].indexOf(value.substring(0, 2)) != -1) {
        return (this.optional(element) || false);
    }
    if (value.length < 10 || value.length > 11) {
        return (this.optional(element) || false);
    }
    if (["6", "7", "8", "9"].indexOf(value.substring(2, 3)) == -1) {
        return (this.optional(element) || false);
    }
    return (this.optional(element) || true);
}, 'Informe um celular válido'); 

 //Telefone fixo
 jQuery.validator.addMethod('telefone', function (value, element) {
        value = value.replace("(", "");
        value = value.replace(")", "");
        value = value.replace("-", "");
        value = value.replace(" ", "").trim();
        if (value == '0000000000') {
            return (this.optional(element) || false);
        } else if (value == '00000000000') {
            return (this.optional(element) || false);
        }
        if (["00", "01", "02", "03", , "04", , "05", , "06", , "07", , "08", "09", "10"].indexOf(value.substring(0, 2)) != -1) {
            return (this.optional(element) || false);
        }
        if (value.length < 10 || value.length > 11) {
            return (this.optional(element) || false);
        }
        if (["1", "2", "3", "4","5"].indexOf(value.substring(2, 3)) == -1) {
            return (this.optional(element) || false);
        }
        return (this.optional(element) || true);
    }, 'Informe um telefone válido'); 

To use just add the methods in your script and add inside the input class (cell or phone) or if you want to validate by the plug-in method use (phone:true or mobile:true)

Making an addendum, the creation of these methods was inspired by the question of thislink.

  • 1

    You can include the list of valid Ddds to make your validation even more top.

  • The post would be clearer if you enumerate in your post the criteria used to differentiate the two.

  • The validation was cool, but in several places the prefix no longer serves as validation of fixed or cellular. For example in SP have cell phones starting with prefix 3!

  • is it possible to format it still in input? ex: you pass 21888888 and it gets 21 8888 8888?

2

At the end choose to use Cleave.js and regex for DDD. Cleave.js is a very modern and easy-to-use Formatter, which uses the library Phonenumber google, but it does not validate the DDD. Together with this regex, vcs are guaranteed.

I created this regex based on the wikipedia list

^0?(1[1-9]|2[12478]|3[1-8]|4[1-9]|5[13-5]|6[1-9]|7[13-5]|7[79]|8[1-9]|9[1-9])

Browser other questions tagged

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