-4

I have a form with phone number field, i need to validate it with the format on keypress. How to do this?
It will be like, some one enters a special characters except ( ) - or alphabet then and there it should show an alert, and if numeric digits are entered then it should get auto formatted like (999)-999-9999.
Can anyone help me how to do this in jquery or javascript??

Edit
I used this code below for jquery mask plugin
$(#myid).mask("(999)-999-9999",{placeholder:" "}); placeholder is used to replace the "_" underscores with blank. This works perfectly now.

Kara
  • 6,115
  • 16
  • 50
  • 57
Rahul
  • 364
  • 3
  • 8
  • 21
  • @andyb i tried with regex but was unable to update it on keypress with the formatting, ie by adding the ()- automatically in the format. i was getting some improper behavior on backspace on keypress event. – Rahul Jun 18 '12 at 06:09

2 Answers2

4

You could use a plugin like jQuery Masked input plugin

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
  • Thanks for your quick help, i used the masked plugin, and its work perfectly as i wanted, even the editing is also easy in the jquery for any other format checking. thanks @F.Calderan – Rahul Jun 18 '12 at 06:11
1

Masked Input Will do the trick

////$("#your_text_box_id").mask("<FORMAT YOU WANT>");
$("#textbox id").mask("99/99/9999");
Ankur Verma
  • 5,793
  • 12
  • 57
  • 93