2

Its not working. I don't know regEx, but I need use it.

if ($('input[name="due_date"]').val().match("^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d\\d$")) {
  $('input[name="due_date"]').after("<span class='v_error'>Must fill</span>");
}
Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Gereltod
  • 2,043
  • 8
  • 25
  • 39
  • Could you be more specific about “not working”? Maybe you just need to insert `.value` in front of `.match`? – Christopher Creutzig Aug 24 '11 at 07:28
  • 1
    Can i ask why you need to use regex? This is better left to date and not regex. too many corner cases. Check this post out http://stackoverflow.com/questions/511439/custom-date-format-with-jquery-validation-plugin – Matt Aug 24 '11 at 07:30

3 Answers3

3
$('input[name="due_date"]').val().match.......
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • No error msg, but when I add alert(), its alerting "Null" msg. – Gereltod Aug 24 '11 at 07:34
  • 1
    @Gerelt Od: Null means no match. Try alert($('input[name="due_date"]').val()). – xdazz Aug 24 '11 at 07:35
  • val() working, its alerting input value. When I input correct value, its still alerting NULL. – Gereltod Aug 24 '11 at 07:37
  • Found it 8/24/2011 returning null. But 08/24/2011 returning value. How I modify my regEx for work first case? – Gereltod Aug 24 '11 at 07:42
  • 1
    Try `"^(0?[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d\\d$"` – xdazz Aug 24 '11 at 07:43
  • 1
    @GereltOd let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2795/discussion-between-xdazz-and-gerelt-od) – xdazz Aug 24 '11 at 07:50
  • Changed double slashes into single slashes at end of regEx. "^(0?[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$" Working well :) Thanks million times. – Gereltod Aug 24 '11 at 07:50
2

A regex is surrounded with slashes. I just found that your regex is incorrect, too... So, coupled with the jQuery error pointed out by xdazz:

$('input[name="due_date"]').val().match(/^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$/);

The regex is from this website.

William Niu
  • 15,798
  • 7
  • 53
  • 93
  • Its working, but now I need new regEx. I need regEx that works both 03/03/2011 and 3/3/2011 cases. If thats not possible at least I need regEx that works 3/3/2011 case. – Gereltod Aug 24 '11 at 07:48
  • 2
    of course it's possible...but it seems you already got your question answered. – William Niu Aug 24 '11 at 07:55
1

You're trying to match with the HTML object, you might add .val() after the jQuery Selector, like

$('input[name="due_date"]').val().match(/^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d\\d$/);
yvan
  • 938
  • 9
  • 18
  • I forgot, but, you don't have to put the regex betweens quotes (") check my code again, I edited it. – yvan Aug 24 '11 at 07:47