Reputation: 776
I am trying to create JavaScript pattern to see if input value is date, my date format is 2015/Jan/01, I have tried doing this \d{4}/\[A-Za-z]{3}/\d{1,2}
but that did not work, Please help me in fixing it.
function SetValue() {
// var newdata = document.getElementById('txtCellEditor').value;
var myString = txtCellEditor.value;
if (myString.match(\d{4}/\[A-Za-z]{3}/\d{1,2})) {
alert("'myString' is date.");
}
Upvotes: 0
Views: 64
Reputation: 87203
Your regex
is incorrect
regex
i.e. /
at the start and end of regex^
and $
to check exact match instead of substring/
by preceding them with \
[
test()
instead of match
Visualization
var regex = /^\d{4}\/[a-z]{3}\/\d{1,2}$/i;
function SetValue() {
var myString = document.getElementById('date').value;
if (regex.test(myString)) {
console.log("'myString' is date.");
} else {
console.log("'myString is not date.");
}
}
<input type="text" id="date" onkeyup="SetValue()" />
Upvotes: 3
Reputation: 1074148
Most of the actual regular expression is fine, but you've forgotten to make it a regular expression by putting it in /
s! (And you've escaped one of the /
[but backward], but not the other.)
if (myString.match(/\d{4}\/[A-Za-z]{3}\/\d{1,2}/)) {
// ^ ^^ ^^ ^
// regex quote-/ \--escaped--/ \-- regex quote
Upvotes: 2