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.
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.
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 .
After a few hours analyzing everything we have about Brazilian phones, I've created a function to validate phones, including:
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.