0

I'm trying to have a user enter their phone number in my Rails 4 app. I'd like it to work like many of the sites that have it like this:

1 turns into (1

(111 turns into (111)

(111)2 then turns into (111) 2

(111) 2223 then turns into (111) 222-3

The end result being something like this: (111) 222-3333

Does the only way to do this involve using javascript on every key press for the input box, checking how many integers have been entered and putting parentheses, a space, and the dash where necessary?

EDIT:

It'd probably have to handle extra cases like where a number is pasted in.

4

1 回答 1

2

回答您的问题 - 每次按键都是 - 最直接的方法。

您也可以使用像http://digitalbush.com/projects/masked-input-plugin/这样的现成插件,并且还支持粘贴。试试看。

于 2013-10-08T08:10:56.937 回答