So I made a script that checks on keyup of input field, if the date is valid. If it matches the regular expression, it should add class date-valid so that the borders turn green (added in stylesheet).
My code:
document.getElementById("date-check").addEventListener("keyup", dateValid);
function dateValid() {
var dateinput = document.getElementById("date-check");
// regular expression to match required date format
re = /^\d{1,2}\-\d{1,2}\-\d{4}$/;
if(!dateinput.value.match(re)) {
dateinput.classList.add('valid-date');
}
}
input[type="date"].valid-date {
border: 1px solid green;
}
<input type="date" id="date-check" class="form-control calender-black" name="dbfield52">
The problem is, now it checks if it doesn't match the regular expression and then add the border. But it should add the border if it matches the reg. When I change it to if(dateinput.value.match(re)) it doesn't work.
type="date"and a browser that supports it, the user cannot choose the format of the date, the browser does for him.String.prototype.match()? UseRegExp.prototype.test()when you only needtrue/false