Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery masked input

I'm having a little issue with a masked input with jquery:

I have a phonenumber text input, so I masked it with a little plugin that I got from this site:

Masked-Input-Plugin by Digitalbrush

It's so usefull to mask inputs with non-complex rules, so I used this rule to mask my phonenumber input:

$("#txtHomePhoneNumber").mask("(99) 999-99 99");

Now the problem is when I want to let the user enter two type of phones on the same input, for example, the user can enter a phone number like this:

(02) 222 - 22 - 22

As well as:

(02) 222 - 22 - 2

Now, how should I concatenate this two rules, so that the masked input keeps formatting the input textbox (adding the dashes and parentheses)?

If you know another way or another plugin that could let me get what I want (even if it's without using this plugin), please let me know.

Thanks in advance.

like image 360
lidermin Avatar asked Dec 19 '09 22:12

lidermin


People also ask

How use jQuery masked input plugin?

First, include the jQuery and masked input javascript files. Next, call the mask function for those items you wish to have masked. jQuery(function($){ $("#date"). mask("99/99/9999"); $("#phone").

What is masking in jQuery?

jQuery mask is a jQuery plugin that helps in putting on a mask on the basic HTML input fields and other elements. If the developer wants the input field to take inputs in a certain format only, then they can use the jQuery mask plugin for it.

What is input masking JavaScript?

The JavaScript Input Mask or masked textbox is a control that provides an easy and reliable way to collect user input based on a standard mask. It allows you to capture phone numbers, date values, credit card numbers, and other standard format values.

What is data Inputmask?

In computer programming, an input mask refers to a string expression, defined by a developer, that constrains user input. It can be said to be a template, or set format that entered data must conform to, ensuring data integrity by preventing transcription errors.


1 Answers

You can have the last character be optional. Put a question mark in the mask definition right before the character you want to start the optional input.

.mask("(99) 999-99-9?9")
like image 89
Josh Bush Avatar answered Sep 22 '22 01:09

Josh Bush