Something like input type date - javascript

I need something like input type='date' when i'm clicking the input on mobile to appear the specific datepicker from phone.. Example for iphone
But I want to display the date how I want.. Exactly what I need is to display the date like this: dd/mm/yy

You can try this HTML5 tag:
<input type="date" value="YYYY-MM-DD"/>
where you can set "value" as the default date.
You can also use min="YYYY-MM-DD" and max="YYYY-MM-DD" for maximum and minimum dates.
and as for the mask "DD/MM/YYYY", I recommend using bootstrap-datepicker, where you can have tons of options to mask as you want.

Related

Force datepicker use for mat-input

I have an issue with the native mat-input type date. At the moment if a user manually types in a date that isn't a real date i.e. 02/31/2022 the value of the input isn't set which leaves it with a "required" error. Obviously, that would make sense and does stop the user and makes them change the date to something that does exist. I'd like something a little more user friendly.
The Material DatePicker handles this very nicely
https://material.angular.io/components/datepicker/overview
When this situation happens it rounds it up to the next available date i.e. 02/30/2022 -> 03/01/2022. However, it is very important that I achieve this goal using the native tag.
<mat-form-field required>
<mat-label>Date</mat-label>
<input matInput type="date" formControlName="dateControl"/>
</mat-form-field>
Some ideas that I am researching and will add to this post if I find insight
Disabling typing the date manually (forcing use of the DatePicker UI)
Rounding up the date to a useful date

How to implement input click select range

see my gif, I hope custom input type=datatime-local time format to "yy-mm-dd", so I hope impl datetime input similar click event, when click input value, select specific range, how to impl
There is a the same question:
Is there any way to change input type="date" format?

How to validate HTML5 date format

I want to use the HTML5 date input field
<input type='date' name='customDate'>
So that other users can make use of the build-in datepicker from their browser.
I would like to check if the input is actually in date format. As I found here: Is there any way to change input type="date" format? there is no unique presentation format. For example, in Chrome the input of the date field is given in the form of dd.mm.yyyy and in Firefox 24 or IE 9/10 the date in the input field is presented as YYYY-MM-DD.
My first problem is, how do you tell the user in which format you want him to type in the date? In Firefox I would need something like
<label>Enter Date(YYYY-MM-DD)</label><input type='date'
name='customDate' placeholder='YYYY-MM-DD'>
But this would be wrong for Chrome.
Secondly, how can I check before submission if the current input is in the valid date format (of the browser)?
I know that I could check with PHP after submit if the date format is given as YYYY-MM-DD, but how do you check it with JavaScript before submission?
Use it like this:
<label>Enter Date(YYYY-MM-DD)</label>
<input type='date' name='customDate' placeholder='YYYY-MM-DD' pattern="(?:19|20)[0-9]{2}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9])|(?:(?!02)(?:0[1-9]|1[0-2])-(?:30))|(?:(?:0[13578]|1[02])-31))" >
For all other HTML5 Dates Pattern Please visit this:
http://html5pattern.com/Dates
I think the best way to validate HTML5 date format is to leave the job to the datepicker if one is available.
For instance, the native datepicker of chrome or ios5 does not allow wrong user date input. In chrome it even checks if the date does exists in a human calendar.
If the browser does not have native datepicker (like firefox), then this should be detected with Modernizer and one should use the jQuery UI datepicker. Here is a nice article that explains how to do this.
Unfortunately, the jQuery datepicker does not check if the input is valid.
However, one can set the dateType and constrainInput, but they dont check if the date actually exists, just if the syntax is correct. Note that the jquery validation plugin has troubles with the date attribut.
just change the type of date dynamically with onfocus and onblur and validate like below.
<input type="text" id="startDate" onfocus="(this.type='date')" onblur="{this.type='text'}" name="startDate" placeholder="Start Date(yyyy-mm-dd)"/>
$("#startDate").val()=""
finally your date value will be yyyy-mm-dd
The best way is to use a calendar/datepicker (ie: jquery-ui).
You can specify the format you want and is also userfriendly.
jquery-ui details and examples for datepicker

Prevent user from keying date into HTML5 date field

I have an HTML5 'date' input field on a form. The dates it displayes are limited to a given range.
This works fine.
I need to prevent users from being able to key a date into this field using a keyboard, making them rely on the calendar dropdown provided by the field and using the mouse to click on a chosen date.
How can this be done? Does the 'date' input have any control like this?
Many thanks in advance. :)
Like this:
Javascript/Jquery:
$("input").keydown(false);
HTML:
<input type="Date" />
See the Demo.
Hope this helps.
You can do that by stopping the default work of keyup event.
//HTML
<input id="date" type=date>
//Javascript
document.getElementById("date").onkeyup=function(){
return false;
}
Working jsfiddle http://jsfiddle.net/y5Fpn/

Kendo UI Datepicker disable typing

I want users to be able to change a Kendo UI Datepicker value only through its button and selecting the date from the pop-up. How can I prevent users from typing in the Datepicker textbox? Can I disable the textbox without disabling the whole control?
On your input element add this attribute and value...
onkeydown="return false;"
This will disable typed input and still allow using the calendar control input.
Use the control as below-
#(Html.Kendo().DatePicker()
.Name("FromDate")
.HtmlAttributes(onkeydown="javascript:return false;" })
)
It will disable keyboard typing. Same way other conditions also can be handled.
Find your input element, and disable it
$('#datepicker').attr('disabled','disabled');
( tried it on the kendo demo website http://demos.kendoui.com/web/datepicker/index.html )
you can do it by two ways
//disable kendo ui datapicker click event
$(".k-datepicker input").bind('click dblclick',function () {
return false;
});
//make it readonly
$(".k-datepicker input").prop("readonly", true);
If you want prevent user to typing date in date picker and only select date from pop-up try this code
$(".k-datepicker").find('span').find('input').attr("readonly", "readonly");
Of course, the date and time picker widget should have the option to force input only with UI and not by keyboard... Otherwise it's a recipe for a real DateTime "formating" nightmare !
I am quite surprised that the framework doesn't provide anything for this obvious use case.
I had the same need and got it to work using the following logic:
$("#date").kendoDatePicker({
format: "dd MMMM yyyy"
});
$("#date").attr("readonly","readonly");
That way the user cannot enter a value by keyboard and can only input a well formated date using the dropdown date selection window.
Indeed, the widget does not restrict user while typing in the input. The reason for this behavior is explained here:
Why widget does not restrict typing
Along with all other solutions shared in this thread, the one can create a custom Masked DatePicker, which will restrict the user to a specific date format. Check this how-to demo for more details:
Create Date Masking
**Note that this is not supported by Kendo UI as a built-in feature, hence you will need to use it on your own risk. The good news is that it works pretty good without known side effects.
.HtmlAttributes(new { onkeydown="return false" })
Justin's answer works, but it doesn't prevent a right mouse-click from pasting an incorrect value. You could use the oncontextmenu to prevent a right mouse-click
oncontextmenu="return false;"
This would also prevent the number in the calendar from being copied though.
for tagHelpers just use following
output.Attributes.Add("onkeydown", "javascript:return false;");
I have solved it on HTML level using onkeydown="return false;"
<input id="datePickerPastId" onkeydown="return false;" title="datepicker" style="width: 13%" class="mr-3" disabled />
The GlobalEventHandlers.onkeydown is supported by all browsers.

Categories