format phone no is us style eg: (xxx)xxx-xxx [duplicate] - javascript

This question already has answers here:
Formatting the phone number
(3 answers)
Closed 6 years ago.
How do I format plain 10 digit phone no. to us style (xxx)-xxx-xxxx using javascript regular expression.

This should do it: ^(\d{3})(\d{3})(\d{4})$
Here is a working example:
var testNumber = '1234567890';
var regex = /^(\d{3})(\d{3})(\d{4})$/;
var sub = '($1)-$2-$3';
var usNumber = testNumber.replace(regex, sub);
alert(usNumber);

Related

Javascript only read certain char from input [duplicate]

This question already has answers here:
How to return part of string before a certain character?
(5 answers)
Closed 6 years ago.
var name = prompt("Enter Name,Barry");
document.write(name);
I want to do exactly this, but only print the first three letters of the 'name'
var name = prompt("Enter Name,Barry").slice(0, 3);
document.write(name)
Try this out it may help.

Javascript split number and string and keep both [duplicate]

This question already has answers here:
Javascript and regex: split string and keep the separator
(11 answers)
Splitting a string into chunks by numeric or alpha character with JavaScript
(3 answers)
Closed 6 years ago.
I have a string
var houseNumber = '13a';
I want to split the addition from the number so I can keep it in a other field.
How can I split this value without losing the number or the addition? At the end I would like to have 2 fields with the following type:
var houseNumber = 13; //Number
var addition = 'a'; //String
There are many questions about this, but I can't find one where both values has to be saved. That's why I created a new question.
Use the following code
var string_a = "jkjkhj89898";
var x = string_a.match(/[^\d]+|\d+/g);
console.log(x)
working fiddle.
Thanks

Regular expression for a decimal with a precision of 2 not working [duplicate]

This question already has an answer here:
Javascript RegEx Not Working [duplicate]
(1 answer)
Closed 6 years ago.
The below code is to validate any input of decimal type with a precision of 2.
function check() {
var str = $('#txttest').val();
var patt = new RegExp("^[0-9]+(\.[0-9]{1,2})?$");
var res = patt.test(str);
alert(res);
}
Valid examples:
12
12.00
12.00a
12a
a12
1a2.00
Failed Case:
In some cases, the function is returning wrong values, like 1a2.
Now, please suggest.
Why don't you use JavaScript's toFixed() method?

how to convert to money format in JS [duplicate]

This question already has answers here:
Javascript Thousand Separator / string format [duplicate]
(15 answers)
Closed 6 years ago.
i have a simple int like "77600" I want to convert it to "77 600" (basically i need to add a simple whitespace after thousands).
tmp_total = parseInt(tmp_total,10); //77600
tmp_total = ...here goes some magic...;
$('#chekout_total #total').text(tmp_total);
try this:
tmp_total = parseInt(tmp_total,10); //77600
tmp_total = tmp_total.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1 ');
$('#chekout_total #total').text(tmp_total);
Hope this will help you! :)

how add number to an array in javascript? [duplicate]

This question already has answers here:
How to append something to an array?
(30 answers)
Closed 8 years ago.
I am trying to add a number to an array .
code:
var year = mm[2].value+1;
but it results undefined.
can you guys how to do this?
You need to parse the string to an integer before you add to it:
var dt = "12/02/2012";
var mm = de.split("/");
var year = parseInt(mm[2])+1;

Categories

Resources