Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 date field and placeholder text in Safari

I've got an issue, but I'm not sure if it's something I'm doing wrong, or if this feature just isn't supported yet.

Basically I have the following lines of code:

<input type="text" name="lastname" id="lastname" value="" placeholder="Last name" />

<input type="date" name="dob" id="dob" value="" placeholder="Date of birth" />

I've tested this on the desktop version of Safari and all good, but when I test it on the iPad, the second input doesn't show the placeholder text. Does anyone know if placeholder text is supported on type="date" on the iPad?

Cheers Nick

like image 379
Sniffer Avatar asked Dec 08 '11 11:12

Sniffer


People also ask

Does Safari support input type date?

Safari does not support the HTML5 date and time input types: https://caniuse.com/#feat=input-datetime.

How do I add a placeholder to a date field?

The placeholder attribute does not work with the input type Date, so place any value as a placeholder in the input type Date. You can use the onfocus=”(this. type='date') inside the input filed.

Is placeholder an attribute in HTML5?

Introduced as part of the HTML5 specification, the placeholder attribute “represents a short hint (a word or short phrase) intended to aid the user with data entry when the control has no value. A hint could be a sample value or a brief description of the expected format.”

How do I display a date field in HTML?

<input type="date"> <input> elements of type="date" create input fields that let the user enter a date, either with a textbox that validates the input or a special date picker interface. The resulting value includes the year, month, and day, but not the time.


2 Answers

The iPad is doing the correct thing. The placeholder attribute isn't supported on input elements on type date. It's probably working on desktop Safari because that doesn't support the date type, so that attribute is being ignored and you're left with a plain text field. You could check in the DOM inspector.

like image 143
robertc Avatar answered Sep 16 '22 13:09

robertc


Slight hack bit here goes...

Initially have the field as a text field. When the user focuses on the field switch the fields type to date and re-focus.

tested in ios6

HTML

<input type="text" placeholder="DOB" id="dob" />

JS

<script>

    var textbox = document.getElementById('dob')
        textbox.onfocus = function (event) {
            this.type = 'date';
            this.focus();
    }

</script>
like image 39
sidarcy Avatar answered Sep 18 '22 13:09

sidarcy