0

I am using a jquery ui datepicker. I simply want to set the dateFormat to something like: "dd/mm/yy", so my javascript is:

$("#datepicker").datepicker({
     dateFormat: "dd/mm/yy"
});

And the relevant html is:

<input type='text' id='datepicker'>

However, doing this restricts the text that can be entered into the input element (I can't enter "-" or "." for example).

http://jsfiddle.net/5sVpA/3/

I need this functionality because I want users to be able to also freely enter their date with some other "date delimiting" options (eg: "5-5-16"). This is not possible because the "-" symbol cannot be entered after specifying the "dateFormat" option.

Is there a way to fix this or is it a bug?

theyuv
  • 1,556
  • 4
  • 26
  • 55

3 Answers3

1

I found an answer. There is a "constrainInput" option that must be set to false. So:

$("#datepicker").datepicker({
     dateFormat: "dd/mm/yy",
     constrainInput: false
});
theyuv
  • 1,556
  • 4
  • 26
  • 55
0

You can do something like this,you need to specify formats in parseDate method

  $.datepicker.inputFormats = ["dd-mm-yy", "dd/mm/yy", "dd mm yy"];//list formats to be accepted here

$.datepicker.originalParseDate = $.datepicker.parseDate;

$.datepicker.parseDate = function (format, value, settings) {
var date;

function testParse(format, value, settings) {
    if ( ! date ) {
        try {
            date = $.datepicker.originalParseDate(format, value, settings);
        } catch (Error) {
        }
    }
}

testParse(format, value, settings);
for(var n = 0, stop = $.datepicker.inputFormats ? $.datepicker.inputFormats.length : 0; n < stop; n++){
    testParse($.datepicker.inputFormats[n], value, settings);
};
return date;
};

The above code is from here https://stackoverflow.com/a/18839641/4810628

Community
  • 1
  • 1
user786
  • 3,902
  • 4
  • 40
  • 72
0

i got the solution for your post.

try this code.

$('#txtDatePicker').datepicker({
    dateFormat: 'dd-mm-y'
});
Amarnath Balasubramanian
  • 9,300
  • 8
  • 34
  • 62
Neo Vijay
  • 823
  • 9
  • 13
  • You didn't understand my question. I would like the freedom to type a variety of symbols in the text field (eg: be able to type all formats: "5-5-16", "5/5/16", "5.5.15" by hand) as well as have the datepicker output the format "dd/mm/yy" – theyuv Jun 04 '15 at 10:21
  • ohhh my god, try this below code, its may be help to you, $('#txtDatePicker').datepicker({ dateFormat: 'dd/mm/yy', constrainInput: false // its allow manual date entry }); please find below url for more. http://stackoverflow.com/questions/5868843/jquery-datepicker-date-manual-input – Neo Vijay Jun 04 '15 at 10:45