Cain Nuke
Cain Nuke

Reputation: 3079

How to limit the number of characters displayed inside input text?

I have a PHP string I CANT CHANGE that produces an input with a date like this:

<input type="text" value="2016-07-12 00:00:00" maxlength="10">

I dont need the 00:00:00 part so I used maxlength to cut it off but it still displays. What I can do to take it off? I cant change the PHP so I have to rely on CSS and HTML solutions only.

Upvotes: 0

Views: 960

Answers (2)

Diptesh Atha
Diptesh Atha

Reputation: 901

Set id or any selector to your text box and place below js code.

document.getElementById("dateField").value = new Date().toISOString().substring(0, 10);

Upvotes: 0

hityagi
hityagi

Reputation: 5256

If you want to achieve this using CSS then read this.

Using javascript you can achieve this by following methods:

var val = document.getElementById("myDate").value;
document.getElementById("myDate").value = val.substring(0, val.indexOf(" "));
<input type="text" value="2016-07-12 00:00:00" maxlength="10" id="myDate">

var val = document.getElementById("myDate").value;
document.getElementById("myDate").value = val.split(" ")[0];
<input type="text" value="2016-07-12 00:00:00" maxlength="10" id="myDate">

var val = document.getElementById("myDate").value;
var maxLen = 10;
document.getElementById("myDate").value = val.substring(0,maxLen);
<input type="text" value="2016-07-12 00:00:00" maxlength="10" id="myDate">

Upvotes: 2

Related Questions