How to add the hyphen in html textbox?

4.3k Views Asked by At

I use the following code to create the textbox for enter the phone number in run Time .How to add hyphen(-) in between the phone Numbers.For example If I type 1234567 the entry should show like this 123-45-67 how to do this? how to do this in JQuery?

<input id="abcd" type="text" value="" style="width:220px">
2

There are 2 best solutions below

1
Cat On

You have to use JavaScript for this. I think this is a good starting point... Bottom line is, though, you will have to do some scripting for this. (Basically, when the field is changed, check if it's the right amount of #s, and add a hyphen if necessary.)

I would recommend jQuery for this; a quick Google turns up some useful results.

Keep in mind that phone numbers from different parts of the world will be hyphenated differently. The USA, for example, uses +x-xxx-xxx-xxxx, while Argentina uses +xx-xx-xxx-xxxx. Some european countries use +xxx-xxxx-xxxx.

0
TRR On

you can do it with the help of Mask. Check this link on how to Mask the textbox data as desired.