If you want to validate a number, use a Regular Expression. The website of Mozilla presents a excellent article on the subject, and the website Regexr is great for validating regular expressions.
In your case, if you type the number with the parentheses and strokes, 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 whitespace at the beginning or end of your String. Or you can clear it using a call to the TRIM function, or you can take the $
\\( e ) represent the specific character input ( and ). It is used because it "escapes" the character instead of making it have its normal function
The symbol - does not need 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 (from 00 to 99)
( (Expression) | (Expression) Represents a Logical OR, where it validates an expression OR another, or BOTH.
If you type without the strokes, the expression will be
^\\([0-9]{2}\\)((3[0-9]{7})|(9[0-9]{8}))$
I believe that in both cases you can assign the regular expression to a variable and initialize a Regexp, or start directly through an attribute passage 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 in the view in a reactive way (as well 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 data structure, which would facilitate its manipulation, and the number would be formatted automatically only when presented to the view (ideal situation).
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 of this tool.
Edit: You are using the Jquery Mask Plugin and, in it, you are cleaning the code of the parentheses and strokes used by the user, and applying the mask directly by the amount of numbers that the user has typed. Therefore, Regex will not work because it is checking for traces or parentheses.
To work 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 type or not the parentheses and the dash will not modify at all the result of the code since it clears the characters that are not numbers anyway through the String filter.replace.
Here has a very good answer about it. It doesn’t help you?
– bio
Wouldn’t it be better to do this through a regex? ([0-9]{2})[0-9]{9}$ validates any String in the format (DDD)Number, including parentheses. If you want to validate a Landline phone, you have to start with 3, then it would be ([0-9]{2})3[0-9]{7}$ and if it is a Landline phone starts with 9, then, ([0-9]{2})9[7-9][0-9]{7}$
– Marcos de Andrade
@Marcosdeandrade What would this look like in regex? only replace the value that is in intRegex ?
– Leonardo Macedo
@bio I will test this your validation
– Leonardo Macedo
The site of Mozilla has an excellent reference on Regular Expressions, suggest take a look. I suggest also look at the site Regexr.com to validate its regex, it is excellent and has a very good references on the side.
– Marcos de Andrade
@I think AP is trying to validate an invalid number. That is, numbers that the user puts as
(11) 1111-11111
. Unfortunately the regex he is using does not validate(12) 3456-7890
, for example. I think that’s it, no?– bio
@bio that’s right, the sequence of (11) 1111-11111 is checking, what it doesn’t check is the sequence of '(12) 3456-7890'
– Leonardo Macedo
Is he typing the dash? The regex I wrote doesn’t use the dash, so it wouldn’t even validate. Let me understand the problem, you want to validate numbers fixed and mobile phones, Only Fixed or Only mobile? If you want to validate FIXED and CELLULAR at the same time, the regex has to use the expression OR, ai é mais difícil de montar.
– Marcos de Andrade
@Marcosdeandrade I need it to validate both because if I put the sequence (01) 2345-6789 is fixed if I put (01) 23456-7890 is cellular only that the two are invalid, gave to understand what I mean?
– Leonardo Macedo
Se você não for digitar o traço, utilize a regex ^([0-9]{2})((3[0-9]{7})|(9[0-9]{8}))$

Se você for digitar o traço, utilize a regex ^([0-9]{2})((3[0-9]{3}-[0-9]{4})|(9[0-9]{3}-[0-9]{5}))$
– Marcos de Andrade
@Would this regex look like this? var intRegex = / ([0-9]{2})(3[0-9]{3}-[0-9]{4})|(9[0-9]{3}-[0-9]{5}))$/; or is it wrong? sorry question do not understand much about regex
– Leonardo Macedo
I put the regex just above your comment. I’m doing a post with the answer. As you are typing a String it has to have double quotes starting and ending, or you type straight into New Regexp(regex)
– Marcos de Andrade
@Marcosdeandrade Ok, I’ll be waiting if post, man thanks so much for your help
– Leonardo Macedo