Validation for xxx-xxx-xxxx or (xxx)xxx-xxxx [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
A comprehensive regex for phone number validation
Whats the regex for
xxx-xxx-xxxx
or
(xxx)xxx-xxxx
I can create regex for the first one with
/^\d{3}-\d{3}-\d{4}$/
but how to add rule in this so it can also handle the second one?

You can use this: -
/^(?:\(\d{3}\)|\d{3}-)\d{3}-\d{4}$/

I'm no guru but this should do the trick;
/^(\d{3}\-)?(\(\d{3}\))?\d{3}\-\d{4}$/

/^(\(\d{3}\))|(\d{3}-)\d{3}-\d{4}$/

Related

Javascript - multiple regular expression in split method [duplicate]

This question already has answers here:
How do you match multiple Regex patterns for a single line of text in Java?
(5 answers)
Closed 3 years ago.
'i-love_sushi_andNoodles'.split(/[-_ ]/);
'i-love_sushi_andNoodles'.split(/(?=[A-Z])/);
Basically, I want to combine above two logics as one. and expect results as
["i", "love", "sushi", "and", "Noodles"]
I feel like there must be easy way to do this.
Please help me out
Thanks,
console.log('i-love_sushi_andNoodles'.split(/[-_ ]|(?=[A-Z])/));

Number format with regex javascript [duplicate]

This question already has answers here:
Javascript Thousand Separator / string format [duplicate]
(15 answers)
Closed 8 years ago.
I have a number is 1205000000, I want display at 1.205.000.000
number.toString().replace(/(\d{3})/g, "$1.").toString()
but result is 120.500.000.0
I don't want reverse a number.
For the sake of correcting your regular expression (obviously for integer values only):
number.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1.');
ยป Detailed Regex Explanation
One way would be to reverse the string before your manipulation and ther reverse it again. Like so:
var number = 1205000000;
function reverse(s) {
return s.split("").reverse().join("");
}
var str = reverse(reverse(number.toString()).replace(/(\d{3})/g, "$1."));
alert(str);
See this working fiddle.
EDIT:
See the comments. Its a bit dirty but for that specific number it will work. The link posted by #Artyom Neustroev as a comment under you question seems a whole lot better than this here.

regex for phone number format (with dot) [duplicate]

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 8 years ago.
Can somebody give me the regex for validating the phone numbers 000.0000.000000 and also without the dots.
sample numbers
880.1817.087511
and
8801817087511
try this.
\b\d{3}[.]?\d{4}[.]?\d{6}\b

Javascript telephone number validation [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Phone Number Validation Javascript
Hi I am creating validation for a form using javascript.
I am wanting to check the telephone number field using if statements to ensure it contains a 4 digit area code then a space followed by a 7 digit number.
Any help would be appreciated.
Thanks
So you need to validate something like 1234 1234567
function isValidTel(numberToTest){
var pattern =/^\d{4} \d{7}$/
return pattern.test(numberToTest);
}

regular expression phone number [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
A comprehensive regex for phone number validation
Can somebody give me the regex for phone number. It can be entered in the format of XXX-XXX-XXXX or (XXX)-XXX-XXXX or XX-XXX-XXXX or XXXXXXXXX
Heres what I have:
/\d{2,3}-\d{3}-\d{4}$/
Brute force/easily maintained/easily read/I think is what you have.
(\d\d\d\-\d\d\d\-\d\d\d\d)|(\(\d\d\d\)\-\d\d\d\-\d\d\d\d)|(\d\d\-\d\d\d\-\d\d\d\d)|(\d\d\d\d\d\d\d\d\d)
Or if you prefer:
(\d{3}\-\d{3}\-\d{4})|(\(\d{3})\-\d{3}\-\d{4})|(\d{2}-\d{3}\-\d{4})|(\d{9})
((\d{2,3}|\(\d{3}\))-\d{3}-\d{4}|\d{9})
Mostly you care about the digits:
/^([()-]*\d)[()-]*){9,10}$/.test(string);
or even
/^(\D*\d\D*){9,10}/.test(string);

Categories

Resources