Gereltod
Gereltod

Reputation: 2234

Validate mm/dd/yyyy with regEx

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>");
}

Upvotes: 2

Views: 4620

Answers (3)

yvan
yvan

Reputation: 958

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$/);

Upvotes: 1

William Niu
William Niu

Reputation: 15853

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.

Upvotes: 2

xdazz
xdazz

Reputation: 160853

$('input[name="due_date"]').val().match.......

Upvotes: 3

Related Questions