Reputation: 2441
i'm currently using this regex
"/^([0-9\(\)\/\+ \-]*)$/",
which is fine,but the problem is it also accepts "blank" inputs and space only,and it messes up my validation, what regex code can allow me to verify only this type of input from the use
(999) 999-9999
and also not accept a "blank" input field from user when entered. any length is fine, as long as it only accepts this inputs that i mentioned above.
outher failed attemps:
^\(\d{3}\)\s{0,1}\d{3}-\d{4}$
/^\(\d{3,4}\) \d{3}-\d{1,10}$/
this are my other failed attemps on making regex for this format. both does not accept
(999) 999-9999
Upvotes: 1
Views: 8437
Reputation: 21
This worked for me. Tested on Regex Tester.
\([1-9][0-9]{2}\)[ ][1-9]{3}[-][0-9]{4}
This will for exact match (999) 999-9999 format.
Escape the '\' as needed in the code.
Upvotes: 1
Reputation: 1697
In JavaScript, use this below:
function validatePhoneNumber(number) {
var re = /^\(\d{3}\)\s{0,1}\d{3}-\d{4}$/;
return re.test(number);
}
In PHP:
<?php
if (preg_match("/^\(\d{3}\)\s{0,1}\d{3}-\d{4}$/", $number)) {
//phone number is valid code
}
?>
Upvotes: -2
Reputation: 1759
You haven't said what language you're doing this in, so we don't know what kinds of limitations are placed on your regex parser. For example, traditional sed
is limited to BRE, awk and grep can handle ERE, and grep on linux systems will handle PCRE. Perl will do PCRE also, of course, as will Python and PHP. But JavaScript only supports a subset of BRE, and misses some of the classic shortcuts for POSIX classes.
So ... to do this in BRE, which everything supports, your brackets are taken literally, and you have limited range controls:
^([[:digit:]]\{3\})[[:space:]]\{0,1\}[[:digit:]]\{3\}-[[:digit:]]\{4\}$
But this is arduous and painful. You're probably using something that at least supports ERE:
^\([[:digit:]]{3}\)[[:space:]]?[[:digit:]]{3}-[[:digit:]]{4}$
And if your parser supports class shorthand, you can shorten this further:
^\(\d{3}\)\s?\d{3}-\d{4}
This last one could be used with perl or PHP's PCRE support, but not with sed, awk or grep.
Also, I'll point out that this is not a good way to handle input validation. You want more flexibility. It would be a better idea to, for example,
You probably would need to come up with some other conditions too.
Upvotes: 2
Reputation: 2441
came up with the solution, the regex everyone was suggesting doesn't accept the "(" and ")"
so i did this
"/^\.{1}\[0-9]{3}\.{2}\[0-9]{3}\-\[0-9]{4}$/",
so that it can accept the "("and ")" for the (999) 999-9999 input
the reason i only require this input, is because i have a script for auto masking, every input from the user will be converted to this format, that is why i needed that specific format from the regex validation.
Upvotes: -2
Reputation: 354784
Don't confuse input validation with data extraction.
Generally forcing an exact format for something like addresses or phone numbers is futile. First of all, You let a human (the one entering the data) doing a computer's job (formatting the data) and even if that works there might be edge cases that won't fit. Not being able to enter valid data is much worse than having a few typos on your end, I believe. For example you're rejecting anything that uses a country prefix.
So for simple validation I'd just look whether there are numbers in the input, nothing more. Just like I would just look for @
to validate an email address. You spend days to get a regex right only to notice that in the end it just doesn't matter.
Upvotes: 1
Reputation: 1704
You forgot to escape the parentheses. Try this: ^\(\d{3}\)\s{0,1}\d{3}-\d{4}$
Upvotes: 1
Reputation: 2807
Use a +
instead of *
to avoid blanks.
In order to avoid "space-only" you can try something like:
"/^([ ]*[0-9\(\)\/\+\-]+[ ]*)*$/"
Upvotes: 0