How to validate the Jquery UI- Calendar plugin? - jquery-ui

I have used JqueryUI plugin- date picker to display my date.
The format is such that it has Fromdate & Todate.
If I select fromDate: for example 09/28/2010 and
If I select toDate: for example 09/30/2010. It should be like this such that todate is
always the one which comes after the fromdate.
But i want to validate such that if I click toDate that is from previous month
or the day before fromDate the validation should not allow me to select a date which is
previous that of fromDate.
My Jscript is
<script type="text/javascript">
$(document).ready(function() {
$('.fromDate').datepicker({clickInput:true});
$('.toDate').datepicker({clickInput:true});
});
</script>
My html is
<tr><td>Enter FromDate:</td><td><input type="text" id="fromDate" class="fromDate"
readonly /></td></tr>
Enter ToDate:
How to do that. Any suggestions Please.

You can use a custom validation method for jQuery Validate for that. See this answer here on SO.

Related

rich:calendar ignored format pattern for the day of the week after selecting

As a part of , I am using component with settled value of datePattern as follows:
<rich:calendar id="effectiveDate"
value="#{itemBean.effectiveDate}"
datePattern="EEEE, MM/dd/yyyy" />
So when I load the form, the existing effectiveDate from the requested itemBean is properly displayed in the text field.
Problems:
when clicking on calendar icon, the content of the text field will be gone. So if I didn't select anything from the calendar popup, then the text field remains empty.
when the date is selected it is displayed like that (for example):
EEEE, 11/19/2012
So the week day is not displayed properly.
Noticed that without specifying the week day (EEEE part of datePattern) none of mentioned above problems occurs.
Is there some specific way to display the week day after selecting?
I am on RichFaces 4.2.2.
Thanks!
As I think this is a known issue with <rich:calendar>. I don't know much about that.
However try this,
<rich:calendar id="effectiveDate" value="#{itemBean.effectiveDate}" onchanged="formatCalendarString(event, this);" datePattern="EEEE, MM/dd/yyyy" />
Within <head></head> tags put this.
<head>
<script>
function formatCalendarString(event, cal) {
var date = new Date(Date.parse(event.rich.component.getSelectedDate()));
var weekdays=new Array(7);
weekdays[0]='Sunday';
weekdays[1]='Monday';
weekdays[2]='Tuesday';
weekdays[3]='Wednesday';
weekdays[4]='Thursday';
weekdays[5]='Friday';
weekdays[6]='Saturday';
var day = weekdays[date.getDay()];
if(typeof(day) !== 'undefined') {
var formattedDate = (day + ', ') + event.rich.component.getSelectedDateString().substring(6);
cal.value = formattedDate;
}
}
</script>
</head>

How to make jqueryUI datepicker submit in a different format than what is being displayed?

I'm working on some internationalization using jQueryUI. I have a DatePicker control on a form that is properly working in the French language.
When I select a date, for example August 15, 2012, the DatePicker will display 15 Aoû, 2012 as I would expect. My issue however, is that when the form is posted, the value of the DatePicker is posted as '15 Aoû, 2012' which now needs to be translated on the server before it can be saved properly.
My question is, is there a built-in way inside the jQueryUI DatePicker so that I can have it always post to the server in a consistent format, regardless of which language the control is being displayed in? If there isn't a built-in way, what options exist for achieving this?
I realize that I can change the dateformat to something like 08/15/2012 instead of using the textual representation, however this isn't what I want to do.
There's 2 configuration options for that: altField and altFormat. http://api.jqueryui.com/datepicker/#option-altField
If you specify an altField, that field will be updated too, and will have the altFormat.
Normally you will want make the altField a hidden field, soyou can ignore the regular field and send to db the altField.
As you'll have noticed, supplying a dateFormat works well for newly entered dates, but it does not alter the value attribute which was already supplied to the date input field. It took me some time and I'm not sure whether this solution is ideal, but here's my situation explained and the code which solves it. Might help others with the same problem in the future. In my example I'm using dd/MM/yyyy as the display format.
The page contains any number of date input fields, which may or may not already have a value attribute supplied in the format yyyy-MM-dd, as specified by W3C.
Some browsers will have their own input control to handle dates. At the time of writing, that is for instance Opera and Chrome. These should expect and store a date in the abovementioned format, while rendering them according to the client's regional settings. You probably do not want/need to create a jqueryui datepicker in these browsers.
Browsers which don't have a built-in control to handle date input fields will need the jqueryui datepicker along with an 'alt', invisible field.
The invisible, alt input field with the yyyy-MM-dd format must have the original name and a unique id in order for forms logic to keep working.
Finally, the yyyy-MM-dd value of the display input field must be parsed and replaced with its desired counterpart.
So, here's the code, using Modernizr to detect whether or not the client is able to natively render date input fields.
if (!Modernizr.inputtypes.date) {
$('input[type=date]').each(function (index, element) {
/* Create a hidden clone, which will contain the actual value */
var clone = $(this).clone();
clone.insertAfter(this);
clone.hide();
/* Rename the original field, used to contain the display value */
$(this).attr('id', $(this).attr('id') + '-display');
$(this).attr('name', $(this).attr('name') + '-display');
/* Create the datepicker with the desired display format and alt field */
$(this).datepicker({ dateFormat: "dd/mm/yy", altField: "#" + clone.attr("id"), altFormat: "yy-mm-dd" });
/* Finally, parse the value and change it to the display format */
if ($(this).attr('value')) {
var date = $.datepicker.parseDate("yy-mm-dd", $(this).attr('value'));
$(this).attr('value', $.datepicker.formatDate("dd/mm/yy", date));
}
});
}
<input type="text" name='fieldName' id="datepicker" value="" style="width: 100px;" />
<script type="text/javascript">
$( "#datepicker" ).datepicker( "option", "dateFormat", 'dd/mm/yy' );
$( "#datepicker" ).datepicker();
</script>
It appears someone else had this question or a similar one prior to yours.
If you read this stackoverflow answer, the author was trying to show the date in one format and pass the data to MySQL in another format.
The prior answer in that link gets you set up to access the selected value as a variable. Now all you need is to wire in a parseDate to your selected date variable.
<script type="text/javascript">
$('#cal').datepicker({
dateFormat: 'dd M yy',
onSelect: function(dateText, inst) {
var dateAsString = dateText; //the first parameter of this function
var newDateFormat = $.datepicker.parseDate('dd-mm-yyyy', dateAsString);
}
});
</script>
Check the parseDate link for settings and formatting.
Hope this helps!
Basically, you should not re-format the date. Instead, you should read JavaScript's Date object from Datepicker, via getDate() method. Then, you need to pass it to server.
The question is how. Basically, what you want is some common format. If you use JSON the answer is very simple, just put date object and JSON's stringify() function will automatically format it to ISO8601.
As you may see from Wikipedia, ISO8601 was designed to interchange date and time reliably, therefore that's what you should use.
It might be helpful to know that modern web browsers support Date object's toISOString() method.
As #Pawel-Dyda mentioned,
There's a
getDate() method
var currentDate = $( ".selector" ).datepicker( "getDate" );
Here's an example of what it returns: Wed Jan 20 2016 00:00:00 GMT+0300.
You can parse it in Javascript, PHP, in SQL or whatever.
MySQL parsing examlple:
select str_to_date('Wed Jan 20 2016 00:00:00 GMT+0300','%a %b %d %Y %H:%i:%s');
Returns:
2016-01-20 00:00:00
Solution working for ASP.NET
#using (Html.BeginForm("ActionName", "ControllerName"))
{
#Html.HiddenFor(m => m.DateFrom)
#Html.HiddenFor(m => m.DateTo)
<div class="form-group">
#Html.LabelFor(m => m.DateFrom)
<input id="datepicker-date-from" type="text" class="form-control datepicker" value="#Model.DateFrom.Date.ToString("dd.MM.yyyy")"/>
</div>
<div class="form-group">
#Html.LabelFor(m => m.DateTo)
<input id="datepicker-date-to" type="text" class="form-control datepicker" value="#Model.DateTo.Date.ToString("dd.MM.yyyy")" />
</div>
<input type="submit" class="btn btn-sn btn-primary" value="Download" />
}
#section scripts {
<script type="text/javascript">
$(function () {
$("#datepicker-date-from").datepicker(
{
dateFormat: "dd.mm.yy",
altField: #Html.IdFor(m => m.DateFrom),
altFormat: "yy-mm-dd"
});
$("#datepicker-date-to").datepicker(
{
dateFormat: "dd.mm.yy",
altField: #Html.IdFor(m => m.DateTo),
altFormat: "yy-mm-dd"
});
});
</script>
}

jquery mobile Mobipick - set min date to 4 days from today

I recently found the mobipick calendar and I really like it due to its fast performance.
However I am having some trouble setting the minimum date four days from now.
If you go on the mobipick website, you can add min="..." in the html attribute, but it doesn't specify today's date.
The following code can be found on the website as well,
how can I achieve this?
Javascript/Jquery
<script type="text/javascript">
$(document).ready(function () {
var picker = $("#picker", this);
picker.mobipick();
picker.bind("change", function () {
var date = $(this).val();
// formatted date
var dateObject = $(this).mobipick("option", "date");
});
});
</script>
HTML
<input id="picker" type="text" />
In order to set the min date four days from now, create an input element
<input type="text" />
and add this JavaScript
$( selector ).mobipick({
minDate: (new XDate()).addDays( 4 )
});
You need version 0.6 for this example to work, download or fork at GitHub https://github.com/sustainablepace/mobipick. I've also added a working example at http://mobipick.sustainablepace.net/demo-advanced.html, see section "Datepicker with dynamic min date".
Let me know if this works for you.

Setting the jQuery UI datepicker's date

Right now when I click on the date control, I start up at the year 2000! How can I tell it to start showing the current month of the current year?
Thanks!
<script type="text/javascript">
$(document).ready(function () {
$('#DOB').datepicker({ defaultDate: null });
});
</script>
With the code above when I click the control I get July 2000 as the starting date.
Set the defaultDate parameter. This works if the attached field is blank. See here:
http://docs.jquery.com/UI/Datepicker#option-defaultDate

jQuery datepicker set selected date, on the fly

How can I change the selected date of jquery Date picker dynamically on the fly?
I have say created a inline date picker.
Then after some time, I want to reflect a different date there without recreating the datepicker from the scratch.
I tried the setDate method, but did not work, and there is not much documentation in the doc.
There is another (extended?) plugin here, but I want to use the plugin which is shipped with jquery.ui.all.js.
What version of jQuery-UI are you using? I've tested the following with 1.6r6, 1.7 and 1.7.1 and it works:
//Set DatePicker to October 3, 2008
$('#dateselector').datepicker("setDate", new Date(2008,9,03) );
Check that the date you are trying to set it to lies within the allowed date range if the minDate or maxDate options are set.
This example shows of how to use default value in the dropdown calendar and value in the text box. It also shows how to set default value to previous date.
selectedDate is another variable that holds current selected date of the calendar control
var date = new Date();
date.setDate(date.getDate() - 1);
$("#datepicker").datepicker({
dateFormat: "yy-mm-dd",
defaultDate: date,
onSelect: function () {
selectedDate = $.datepicker.formatDate("yy-mm-dd", $(this).datepicker('getDate'));
}
});
$("#datepicker").datepicker("setDate", date);
Noted that for DatePicker by Keith Wood (http://keith-wood.name/datepickRef.html) the following works - note that the setting of the default date is last:
$('#datepicker').datepick({
minDate: 0,
maxDate: '+145D',
multiSelect: 7,
renderer: $.datepick.themeRollerRenderer,
***defaultDate: new Date('1 January 2008')***
});
$('.date-pick').datePicker().val(new Date()).trigger('change')
finally, that what i look for the last few hours! I need initiate changes, not just setup date in text field!
For some reason, in some cases I couldn't make the setDate work.
A workaround I found is to simply update the value attribute of the given input.
Of course the datepicker itself won't be updated but if what you just look for is to display the date, it works fine.
var date = new Date(2008,9,3);
$("#your-input").val(date.getMonth()+"/"+date.getDate()+"/"+date.getFullYear());
// Will display 9/3/2008 in your #your-input input
This is a old thread but I just want to give some information how I have used. If you want to set today date you can simply apply it like below.
$("#datepickerid").datepicker("setDate", "0");
If you want to set some days before/after the current date then use the symbol -/+ symbol of how many days you want like below.
$("#datepickerid").datepicker("setDate", "-7");
$("#datepickerid").datepicker("setDate", "+5");
This works for me:
$("#dateselector").datepicker("option", "defaultDate", new Date(2008,9,3));
please Find below one it helps me a lot to set data function
$('#datepicker').datepicker({dateFormat: 'yy-mm-dd'}).datepicker('setDate', '2010-07-25');
setDate only seems to be an issue with an inline datepicker used in jquery UI, the specific error is InternalError: too much recursion.
In Html you can add your input field with a date picker like this
<input class="form-control date-picker fromDates" id="myDate" type="text">
and then in java script, initialize your datepicker and set your value to the date like this,
$('.fromDates').datepicker({
maxDate: '0',
dateFormat: "dd/mm/yy",
changeYear: true,
changeMonth: true,
yearRange: "-100:+0"
});
var myDateVal = moment('${value}').format('DD/MM/YYYY');
$('#myDate').datepicker().datepicker('setDate', myDateVal );
(In here fromdate attribute shows the previous dates of the current date)
var dt = new Date();
var renewal = moment(dt).add(1,'year').format('YYYY-MM-DD');
// moment().add(number, period)
// moment().subtract(number, period)
// period : year, days, hours, months, week...
I had a lot of trouble with the setDate method as well. seems to only work in v1. What does seem to work however is using the dpSetSelected method:
$("#dateselector").dpSetSelected(new Date(2010, 0, 26).asString());
good luck!
or you can simply have
$('.date-pick').datePicker().val(new Date()).trigger('change')

Resources