I put 10 digit mobile numbers in a Textarea like the follwoing. 242452354643663463636346366363463636365636363634656346 but i need to put comma(,) after every 10 digit number.
-
1any example of input and output string?YOU– YOU2010-06-03 06:03:14 +00:00Commented Jun 3, 2010 at 6:03
-
I'm a little unclear if you want to validate that there's a comma after every 10 digits, or insert one automaticallyMichael Mrozek– Michael Mrozek2010-06-03 06:09:48 +00:00Commented Jun 3, 2010 at 6:09
Add a comment
|
2 Answers
Like this?
"242452354643663463636346366363463636365636363634656346".replace(/(\d{10})/g,"$1,")
// 2424523546,4366346363,6346366363,4636363656,3636363465,6346
3 Comments
Hari kanna
Yes like that..Which javascript event would be better we prefer Keyup?
Hari kanna
can u explain in detail?what is g?what is $1?
YOU
@Hari kanna,
/(\d{10})/ is a regular expressions, with 10 continuous digits, and $1 means, matched 10 digits in the bracket ( 10 digits ), add comma after all matched instances. Basically, replacing 10 digits with 10 digits + ","Above solution did not help with splitting a 10 digit phone number being typed into for example a textarea. My solution: 1. your text area should be validated to return only numbers onKeyPress.
then
function commafyPhone(str){
var newStr='';
if(str.length>10){
var str_array=str.split(",");
for(var i = 0; i < str_array.length; i++) {
newStr+=str_array[i].replace(/(\d{10})/g,'$1,');
}
return newStr;
}
return str;
}
On the textarea form field, i used:
onKeyUp="this.value=commafyPhone(this.value);"
However, my solution requires to remove the comma on your last number entered.
1 Comment
Kunvar Singh
how can i remove ',' from end?