format int to phone number

后端 未结 2 946
感动是毒
感动是毒 2021-01-06 09:19

Is there a way I can format for example: 0000000000 into (000)000-0000? I\'m returning a listbox which holds a collection of phone number which arent formated yet. What I wo

2条回答
  •  臣服心动
    2021-01-06 09:50

    As simple as this. Can use a simple if statement if you are going to deal with multiple numbers.

        YourVariable = YourVariable.replace(/(\d{1})(\d{3})(\d{3})(\d{4})/, "+$1 ($2)-$3-$4"); 
    

    Displays: +1 (800)-555-5555

        YourVariable = YourVariable.replace(/(\d{3})(\d{3})(\d{4})/, "($1)-$2-$3"); 
    

    Displays (555)-555-5555

    If statement (my own code):

    if (form.FaxNumber.length = 11){
        form.FaxNumber.value = form.FaxNumber.value.replace(/(\d{1})(\d{3})(\d{3})(\d{4})/, "+$1 ($2)-$3-$4");
    }
    if (form.FaxNumber.length = 10){
        form.FaxNumber.value = form.FaxNumber.value.replace(/(\d{3})(\d{3})(\d{4})/, "($1)-$2-$3");
    }
    

提交回复
热议问题