I'm currently using this RegEx ^(0[1-9]|1[0-2])/(19|2[0-1])\d{2}$
in .NET to validate a field with Month and Year (12/2000
).
I'm changing all my RegEx validations to JavaScript and I'm facing an issue with this one because of /
in the middle which I'm having problems escaping.
So based on other answers in SO I tried:
RegExp.quote = function (str) {
return (str + '').replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&");
};
var reDOB = '^(0[1-9]|1[0-2])/(19|2[0-1])\d{2}$'
var re = new RegExp(RegExp.quote(reDOB));
if (!re.test(args.Value)) {
args.IsValid = false;
return;
}
However, validations fails even with valid data.
Remove ^
of first and $
from end of regex pattern. And add \
before any character which you want to match by pattern. so pattern is like this:
(0[1-9]|1[0-2])\/(19|2[0-1])\d{2}
You can test your regex from here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With