HTML:
<input type='text' value="2012-12-30 Morning">
<input type='text' value="2012-12-30 Lunch">
<input type='text' value="2012-12-30 Dinner">
<input type='text' value="2012-12-30 Either akgalkgalkgla">
<input type='text' value="2012-12-30">
<input type='text' value="Morning">
<button>Check</button>
Javascript/jQuery:
$("button").click(function() {
$("input")
.filter(function() {
return this.value.match(/[0-9]{4}-[0-9]{2}-[0-9]{2}\s[A-Za-z]+/);
})
.css("border", '1px solid red');
})
I tried using the anchors ^ and $ but it doesn’t match it anymore. In the above example, it should NOT match “2012-12-30 Either akgalkgalkgla”
Here’s the regex
[0-9]{4}-[0-9]{2}-[0-9]{2}\s[A-Za-z]+
2012-12-30 Morning
2012-12-30 Lunch
2012-12-30 Dinner
2012-12-30 Either akgalkgalkgla
Update:
So ^[0-9]{4}-[0-9]{2}-[0-9]{2}\s[A-Za-z]+$ does work. I was initially trying this on regexpal, and it didn’t seem to work…
Adding the anchors seems to work perfectly for preventing partial matches. The contents of the first three inputs are matched, the last three are not:
Here it is in action: http://jsfiddle.net/ZZYMG/4/
If you want to apply the border to all the elements that fail this test, simply invert the boolean you are returning in the filter callback: