3

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

2
  • 1
    any example of input and output string? Commented 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 automatically Commented Jun 3, 2010 at 6:09

2 Answers 2

7

Like this?

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

// 2424523546,4366346363,6346366363,4636363656,3636363465,6346
Sign up to request clarification or add additional context in comments.

3 Comments

Yes like that..Which javascript event would be better we prefer Keyup?
can u explain in detail?what is g?what is $1?
@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 + ","
1

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

how can i remove ',' from end?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.