I am trying to "check" to see if an field for MM/YY includes a "/" at the 3rd character, and if not, then using Javascript to add it before the form is submitted. For example, if a user inputs "0120", I'd like this function to submit the form as if the user wrote "01/20".
If the user already inputs "01/20", I'd like it to do nothing. What is the simplest way to do this? My attempt below does not work.
<input id="month-year">
var monthYear = document.getElementById('month-year').value;
if(monthYear.includes("/")) {
//do nothing
} else {
monthYear = monthYear.insert(2,"/");
}
I know there are other questions here about how to validate whether the input pattern includes the "/" or not, but I'm less concerned about validating a pattern just to tell the user "try again" and more concerned with just auto-correcting it on our end so the user doesn't have to (which I can't find).