格式化电话号码

时间:2012-04-02 09:30:32

标签: javascript jquery regex

以下文本框在从数据库获取电话号码后打印电话号码的值。

<table>
<tr>
<td>
<s:textfield theme="simple" name="phoneNumber"/>
</td>
</tr>
</table>

如何以格式(xxx)xxx-xxxx打印此值。 注意:值来自数据库的0123456789形式 输出应为(012)345-6789。

3 个答案:

答案 0 :(得分:2)

我更愿意使用replaceregexp(代码更少,功能更多)。

         var phone = "0123456789";
         phone.replace(/(\d{3})(\d{3})(\d{4})/,"($1)$2-$3"); // (012)345-6789

参考:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace

答案 1 :(得分:0)

phone = "0123456789"
formated_phone = "("+phone.substring(0,3)+")"+phone.substring(3,6)+"-"+phone.substring(6,11)

答案 2 :(得分:0)

int xForBrick = 0;
for(int col = 0; col < numOfCols; col++)
{
    if(col %  5 == 0) //change here
    {
        distRows = distRows + 1; //change here
        xForBrick = 0;
    }
    bricks[col] = new Brick(xForBrick*256,distRows);
    xForBrick++;
}