Auto Reg Ex Phone Number format (xxx) xxx-xxxx [duplicate] - javascript

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 6 years ago.
I found a pretty old example but it's almost doing exactly what i need to do:
Auto Dash using Javascript on FOCUS (for a telephone number format)
However instead of the current format xxx-xxx-xxxx i'd like to do (xxx) xxx-xxxx
Can someone give me a hand with the script below with this??
$('.telnumber').keyup(function() {
this.value = this.value
.match(/\d*/g).join('')
.match(/(\d{0,3})(\d{0,3})(\d{0,4})/).slice(1).join('-')
.replace(/-*$/g, '');
console.log("this value", this.value);
});

You could do something like this
var a = this.value.match(/\d*/g).join('')
.match(/(\d{0,3})(\d{0,3})(\d{0,4})/).slice(1);
this.value = ['(',a[0],') ',a[1],'-',a[2]].join('');
It's not very elegant but hey, elegance is for tailors right?
Update
Yea, I got bored and made a one-liner, here:
this.value = '('+this.value
.match(/\d*/g).join('')
.match(/(\d{0,3})(\d{0,3})(\d{0,4})/).slice(1)
.map((a,i)=>(a+[') ','-',''][i])).join('');

The code you'll want to change is .join('-'). That's where you're getting the 'xxx-xxx-xxxx' from.

You need to tokenize those 3 regular expressions and then join them separately.

Related

Mask a input in HTML using javascript [duplicate]

This question already has answers here:
Mask US phone number string with JavaScript
(9 answers)
Closed 7 years ago.
How can I mask a HTML input text with a mask for phone number?
Here in Brazil, in some states, the number format can change.
For example:
(dd)xxxx-xxxx
this is the default number format, where dd is the state code, and the x's are the numbers.
BUT, in some states, the number nine is added to the phone number, like this:
(dd)9xxxx-xxxx. (let's call it "new format")
the var dd isn't important, what I want to do is when the user type the new format, the hyphen automatically appears after the 4th X.
When the users start entering the phone number, you could have a regex such as ^(\d{2})9 and regex match the string to see if the hyphen needs to be added at a different place.
You could use something like formatter.js to do the job for you!
I have not tried it, but the demos look quite solid.
Try using two inputs with different masking (8 digit & 9 digit respectively) and switch them based on the state selected in the form.

javascript now to create two ways currency format? [duplicate]

This question already has answers here:
How to convert a currency string to a double with Javascript?
(23 answers)
Closed 8 years ago.
I have found this link have a simple and useable funciton to convert number to currency.
www.mredkj.com/javascript/nfbasic.html
but how to turn it back form currency to number...
I already try this :
[http://jsfiddle.net/vb2cb1tm/3/]...
But its failed...
I try to read a formatted string using javascript and make a counting on this valua...
I think something as simple as
function numbers(nStr){
return Number(nStr.replace(/[\$\,]/g,''));
}
might do the trick, though it's hard to tell without seeing your original code.
function cur2num(cur) {
return parseFloat(cur.replace(/,/g,"");
}
if currency signs try /[^0-9]\.\-]/g as first argument to the replace

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.

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