admin管理员组

文章数量:1417070

I put 10 digit mobile numbers in a Textarea like the follwoing. 242452354643663463636346366363463636365636363634656346 but i need to put ma(,) after every 10 digit number.

I put 10 digit mobile numbers in a Textarea like the follwoing. 242452354643663463636346366363463636365636363634656346 but i need to put ma(,) after every 10 digit number.

Share Improve this question edited Jun 3, 2010 at 6:09 Hari kanna asked Jun 3, 2010 at 6:01 Hari kannaHari kanna 2,5216 gold badges30 silver badges44 bronze badges 2
  • 1 any example of input and output string? – YOU Commented Jun 3, 2010 at 6:03
  • I'm a little unclear if you want to validate that there's a ma after every 10 digits, or insert one automatically – Michael Mrozek Commented Jun 3, 2010 at 6:09
Add a ment  | 

2 Answers 2

Reset to default 7

Like this?

"242452354643663463636346366363463636365636363634656346".replace(/(\d{10})/g,"$1,")

// 2424523546,4366346363,6346366363,4636363656,3636363465,6346

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 mafyPhone(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=mafyPhone(this.value);"

However, my solution requires to remove the ma on your last number entered.

本文标签: Input a comma after every 10 digit in JavascriptStack Overflow