Screenshot
How to validate dd-mm-yyyy with regex, currently I have got yyyy-mm-dd but not able to find for dd-mm-yyyy. Please help as I need on validate on every keyup
$("#date").on("keyup", function()
{
let valid = /^d{0,4}$|^d{4}-0?$|^d{4}-(?:0?[1-9]|1[012])(?:-(?:0?[1-9]?|[12]d|3[01])?)?$/.test(this.value), input = this.value;
if(!valid) {
this.value = input.substring(0, input.length - 1);
this.style.backgroundColor = '#EEA39C';
}
setTimeout(() => { this.style.backgroundColor = '#88DD85'; }, 700);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<input id="date" style="font-size: 20px" maxlength="10" type="text" />
2
Answers
You should not use Regex to validate if date is actually valid. Use JS logic for that, e.g. momentJs library
This regex check the whole Gregorian rule. you can try this.