Question

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.

Was it helpful?

Solution

Answering your question - yes on every keypress - the most straightforward method.

Also you could make use of ready plugin like this http://digitalbush.com/projects/masked-input-plugin/ and yes pasting is also supported. Give it a try.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top