Check if value matches mask

Home / Uncategorized / Check if value matches mask

Question:
I have a input that has a mask of date like __/__/____. I stored it in a variable. Is there a way to detect if it matches the mask properly?

The event is binded on blur, so sometimes it is returning values like 22/12/___.

Is there a way to detect if it definitely fits the scheme with integers and slash only or doesn’t?
$(‘#btn’).click(function() {
value = $(‘#my-input’).val() // should be in this format 99/99/9999 and not 12/12/12__

// here I need to check if it is in right format.
if (checkFormat(value)) {

}
}


Answer:
You can use regex to validate this.
var regex_check = /^d{2}/d{2}/d{4}$/ ;

//Edit – Vanilla JavaScript
function foo(date) {
var regex_pattern = /^d{2}/d{2}/d{4}$/ ;
var check = new RegExp(regex_pattern);
var res = check.test(date);

// return true or false
return res;
}
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *