I want to use a regexp to "force" a user to correctly format a timestamp.
At the moment I can restrict input so that the user can only input numbers, a dash, or a colon using the following:
function allownumbers(e) {
var key = window.event ? e.keyCode : e.which;
var keychar = String.fromCharCode(key);
var reg = new RegExp("[-0-9: ]")
if (key == 8) {
keychar = String.fromCharCode(key);
}
if (key == 13) {
key = 8;
keychar = String.fromCharCode(key);
}
return reg.test(keychar);
}
However, the user could still enter invalid data, e.g. 0000::--12354 would validate.
Can I use a regexp to force the user to enter ####-##-## ##:##:## (e.g. 2010-12-15 10:57:01)?
(Even cooler would be if it automatically added the dashes, colons and space when it hit the correct place in the string.)
Cheers,
Ben