If you want to validate a number, use a Regular Expression. The mozilla website features a excellent article on the topic, and the Regexr site is great for validating regular expressions.
In your case, if you enter the number with parentheses and dashes, the expression will be
^\([0-9]{2}\)((3[0-9]{3}-[0-9]{4})|(9[0-9]{3}-[0-9]{5}))$
Explaining:
-
The symbol ^ indicates the beginning of a String, and $ indicates the end of a String. This means that there can be no spaces at the beginning or end of your String. Or you can clear it by using a call to the TRIM function, or you can take the ^ and $
-
\\ (and \\) represent the character input specifically (e). You use \ because it "escapes" the character instead of having it have its normal function
-
The symbol - does not have to be escaped with \ because it has no other function in regular expressions.
-
[0-9] {2} accepts two of any value in sequence between 0 and 9 (hence, from 00 to 99)
-
-
If you type without the dashes, the expression will be
^\([0-9]{2}\)((3[0-9]{7})|(9[0-9]{8}))$
I believe that in either case you can assign the regular expression to a variable and initialize a RegExp, or start straight through an attribute pass to RegExp
var expressao = '^\([0-9]{2}\)((3[0-9]{3}-[0-9]{4})|(9[0-9]{3}-[0-9]{5}))$';
var regex = new RegExp(expressao);
OR
var regex = new RegExp('^\([0-9]{2}\)((3[0-9]{3}-[0-9]{4})|(9[0-9]{3}-[0-9]{5}))$');
After creating the regular expression you can validate it using the match method that regex has.
var telefone = '(31)3233-4343';
var regex = new RegExp('^\([0-9]{2}\)((3[0-9]{3}-[0-9]{4})|(9[0-9]{3}-[0-9]{5}))$');
regex.test(telefone);
In function form:
function validPhone (phone) {
var regex = new RegExp('^\([0-9]{2}\)((3[0-9]{3}-[0-9]{4})|(9[0-9]{3}-[0-9]{5}))$');
return regex.test(phone);
}
validPhone('(31)3534-2323'); //Valido
validPhone('(31)9923-23288'); //Valido
validPhone('(31)9923-3288'); //Invalido
validPhone('(31)2323-5443'); //Invalido
Alternatively , you can use a library that applies a mask directly to the view in a reactive way (such as react, angular, and vue, for example, have applications), and the user would only type the numbers; it would be stored without the characters in your database or database, which would make it easier to manipulate, and the number would be automatically formatted only when presented to the view.
Example: 319938-42838 would be formatted for (31) 9938-42838.
Edit: To validate a DDD between 11 and 99:
var telefone = '(31)9923-99288';
var regex = new RegExp('^\(((1[1-9])|([2-9][0-9]))\)((3[0-9]{3}-[0-9]{4})|(9[0-9]{3}-[0-9]{5}))$');
if (regex.test(telefone)) {
console.log("Válido");
}
else console.log("Inválido");
You can test online through from this tool .
Edit: You are using the Jquery Mask Plugin and it is cleaning the code of the parentheses and traces used by the user, and applying the mask directly by the number of numbers that the user entered. So Regex will not work because it is checking for traces or parentheses.
To work in this way, you can use the following method:
function validatePhone (phone) {
var regex = new RegExp('^((1[1-9])|([2-9][0-9]))((3[0-9]{3}[0-9]{4})|(9[0-9]{3}[0-9]{5}))$');
return regex.test(phone);
}
var telefone = '31992399288';
validatePhone (telefone);
In this case, the user typing in the parentheses and the dash will not change the result of the code in any way since it clears the non-numbers characters anyway through the String.replace filter.