I'm trying to add functionality to input date fields so as when the users enters in digits, slashes "/" get automatically added.
So suppose I have the following html:
<input type="text" id="fooDate" />
And suppose I have the following javascript:
var dateField = document.getElementById("fooDate");
dateField.onkeyup = bar;
What should bar
be?
So far the best google result was:
function bar(evt)
{
var v = this.value;
if (v.match(/^d{2}$/) !== null) {
this.value = v + '/';
} else if (v.match(/^d{2}/d{2}$/) !== null) {
this.value = v + '/';
}
}
Thanks!
also -- I know having slashes being entered as you type sucks. Just roll with it :p
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…