4

I've been using the jquery masked input plugin to apply a date mask to text inputs. This works pretty well, but I want the displayed mask to also show the format. That is, instead of the mask being

__/__/____

I want to be able to specify the format by using a mask of, for example,

dd/mm/yyyy

or (which looks the same with the current mask but is fundamentally different)

mm/dd/yyyy

I can't see how to do this with that plugin (it only accepts one character as the mask character as far as I can see). Is it possible with the current plugin? Does anyone know of an alternative plugin (ideally using jQuery) which can do this? I haven't been able to find anything.

2
  • Think this through a bit better. Suppose you could use dd/mm/yyyy and then someone types in 29/02/200 (last digit not yet typed in). What should the plugin do for the final digit? Reject all inputs except 0, 4 and 8 without providing feedback? There are lots of examples on this theme. Commented Jan 17, 2012 at 15:01
  • Yes, you could validate the date with the mask plugin too - but can you give me an example of a plugin that does this (and has the other behaviour of the mask plugin, such as automatically advancing over the static parts ('/' in the date format)? I already validate the input seperately and am happy with that, what I need is an easy way to tell the end user what format the date should be in. If it can enforce a valid date then great (although I still need to validate that certain dates are before or after others) but that's not a requirment for me Commented Jan 18, 2012 at 6:09

6 Answers 6

7

This is supported by https://github.com/digitalBush/jquery.maskedinput (now archived - used to be at http://digitalbush.com/projects/masked-input-plugin/) - you just have to specify the full placeholder (see the date demo on that site):

$('#date').mask('99/99/9999',{placeholder:"mm/dd/yyyy"});

Sign up to request clarification or add additional context in comments.

2 Comments

Why mask it as 99/99/9999 when no month that reaches 99? Same as day.
@Whisk - Clicking your link gives "Error establishing a database connection"
3

I had a similar requirement recently, and the only thing I could find that didn't require me to do a lot of work on tweaking the plugin was to use this modified date masking plugin. It seems to be doing the job so far.

If you want to test it out, head to this page.

The basic implementation we've used with this modified date masker is as follows:

$('#yourInputBoxHere').mask("99/99/9999", {placeholder: 'MM/DD/YYYY' });

I hope this helps.

Update: A word of warning: this plugin seems to be incompatible with later versions of jQuery. I think it's safe for jQuery 1.4 and lower.

5 Comments

Looks like just what I was looking for. Thanks (Caveat: I have tried it at the pages above but have not had time to implement it myself yet)
A word of warning, though, this plugin seems to be incompatible with later versions of jQuery. I think it's safe for jQuery 1.4 and lower.
Can you update the links? The current ones are dead.
Thanks @VenomFangs. I took a look and was able to successfully access the linked pages as of 7/28/2016.
Or, $('#yourInputBoxHere').inputmask({"mask": "99/99/9999", "placeholder": 'MM/DD/YYYY'});
1

You can set the value of the input as a default value like this:

$('#someId').mask("99/99/9999").val("dd/mm/aaaa");

Comments

1

I had similar requirement and so i was used like below option and it is help me to format with date. You need to load input mask jQuery file as well.

$("#yourInputBoxidHere").inputmask("datetime",{ inputFormat:"dd/mm/yyyy", placeholder: "DD/MM/YYYY"});

This is working for me well. so it will help you as well.

1 Comment

You are using .inputmask() not .mask() - is this a different plugin? Can you edit and add a link to the plugin you are using?
0

You can specify your own mask to this plugin using the $.mask.definitions and then use it in your code.

1 Comment

I know I can specify my own mask. It's not the mask that is the problem it's the placeholder (I already have a mask which works). What I want is something like $('#foo').mask('99/99/9999', {placeholder: 'dd/mm/yyyy'}). But that uses the whole placeholder string for every character, which is not what I want.
0

Sample HTML & Jquery code.

$(document).ready(function () {
            const dateInput = $("#date-input");

            // Function to format the date as YYYY/MM/DD
            function formatDate(value) {
                const numbers = value.replace(/\D/g, ""); // Remove non-numeric characters
                const year = numbers.substring(0, 4);
                const month = numbers.substring(4, 6);
                const day = numbers.substring(6, 8);

                let formattedDate = year;
                if (month) formattedDate += "/" + month;
                if (day) formattedDate += "/" + day;

                return formattedDate;
            }

            // Handle input event to enforce masking
            dateInput.on("input", function () {
                let value = $(this).val();
                const formattedValue = formatDate(value);
                $(this).val(formattedValue);
            });

            // Allow backspace to remove characters properly
            dateInput.on("keydown", function (e) {
                if (e.key === "Backspace") {
                    let value = $(this).val();
                    const newValue = value.slice(0, -1); // Remove the last character
                    $(this).val(formatDate(newValue));
                    e.preventDefault(); // Prevent default backspace behavior
                }
            });

            // Prevent entering invalid characters
            dateInput.on("keypress", function (e) {
                const char = String.fromCharCode(e.which);
                if (!/[0-9]/.test(char)) {
                    e.preventDefault(); // Allow only numbers
                }
            });
        });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div>
            <label for="date-input">Enter Date (YYYY/MM/DD):</label>
            <input type="text" id="date-input" maxlength="10" placeholder="YYYY/MM/DD">
        </div>

I hope this aligns with your requirements!

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.