I've got a particular date and time format I'm trying to live filter in the text field so the users (ideally) can't enter a different format.
On the live filtering, I've been able to limit the characters they can enter, but no luck enforcing the pattern as they type. I know there must be some way to do this with raw JavaScript, I've tried looping over it but so far no luck.
The format that I'm using is dd/mm/yyyy hh:mm am|pm with the time section and leading 0s being optional. So 03/01/1982 3:14 PM would fit, but so would 3/1/1982 The pattern that I've devised (and tested at regex101) seems overly complicated, perhaps that's part of why I'm not having any luck.
Here is the JavaScript I have that works for controlling what can be input, though as stated it doesn't conform the input to the pattern:
<script type="text/javascript">
function format_date(e){
var text = document.getElementById(e);
var regex = /[^\d pam//:]/gi;
text.value=text.value.replace(regex, "");
}
</script>
and the HTML field calling it:
<input type="text" id="dob" name="dob" onKeyup="formate_date('dob')" onKeydown="format_date('dob')">
This seems to be the regex for the pattern I need, allowing for what is optional and accounting for missing leading zeros:
/^[\d]{1,2}\/[\d]{1,2}\/[\d]{4}(\s[\d]{1,2}:[\d]{1,2} (am|pm))?/gi
Can anyone point me to examples or docs that address this type of issue or help me understand what I'm missing? When I've tried including a loop in the function it either throws an error or doesn't enforce the pattern so I'm at a bit of a loss.
Thanks!