How to validate fixed and mobile phone number jQuery Validator?

9

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

Differentiation of fixed-to-mobile numbers is important in order to validate the sending of SMS.

    
asked by anonymous 07.05.2014 / 22:29

2 answers

5

By web development experience, it is very common to see that various forms that validate landline or mobile only address the minimum and maximum number of characters, but in fact there is no more consistent verification to differentiate one from the other. You often need to send an SMS and the cell number is also poorly validated.

For developers using the jQuery Validator plugin

Below are two methods to make the database more complete and to aid in 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, simply add the methods in your script and add them inside the input class (cell or phone) or if you want to validate by the plug-in method use (phone: true or cell: true)

By making an addendum, the creation of these methods was inspired by the link .

    
07.05.2014 / 23:16
4

After a few hours analyzing everything we have about Brazilian phones, I've created a function to validate phones, including:

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

ANATEL has not yet implemented everything, but when someone sees this and is already in 2017 or later (look I'm just talking to someone of the future haha), just delete the line that checks the year, which is referenced in the code. / p>

See a working example: JsFiddle

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

Thanks also to @Leandro Curioso, as I based my code on yours.

    
03.06.2016 / 17:08