views:

584

answers:

3

I am having a text field for date. Now i would like to mask this text field so that a user can input only date value. I even found a jQuery plugin for this. But that doesn't seems to help, as you can input 22/22/2222 in the field that too is accepted by this plugin, which is obviously not a valid date. What is the possible solution?

A: 

You can set the input to be readonly, and put a div around it that has the onclick set to call the calendar.

That way they can't change the value, but if they click on it they can set a new date.

James Black
@James: good answer but obviously not acceptable. i want to mask the field. i don't want user to navigate through the calendar to select what he can write easily. Infact i can leave the plan of masking the input but cannot forbid the user to input manually.
Rakesh Juyal
So just use keypress events and validate each keypress to ensure that it is valid where it is at, currently.
James Black
+1  A: 

try working from this http://digitalbush.com/projects/masked-input-plugin

if that does not work, try adapting from this code, changing ti to work for your months:

    function isZipKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && ((charCode < 48 || charCode > 57) && charCode != 45))
        return false;

    return true;
    }

    onkeypress="return isZipKey(this);"
Ten Ton Gorilla
A: 

If you want valid dates, you'll need to validate each keypress. Not a tiger I'd like to tackle personally. If you're not afraid of JavaScript (or JQuery), why not use a calendar GUI like Datepicker

John Giotta