AKWebApps
AKWebApps

Reputation: 11

javascript regExp works with testers but not in my site

Simple RegExp to test for mm/yyyy works on all online RegExp testers but not on my site.

var re=new RegExp("^(1[0-2]|0[1-9]|\d)\/(20\d{2}|19\d{2}|0(?!0)\d|[1-9]\d)$");
if(!re.test(theForm.ceDate.value)) alert("Date must be MM/YYYY");

Any help would be greatly appreciated.

Upvotes: 0

Views: 79

Answers (2)

Pankaj
Pankaj

Reputation: 10105

JavaScript

function Validation() {
    debugger;
    var reg = new RegExp('^(0[1-9]|1(1|2)|[0-9])\\/(20[0-9]{2}|19[0-9]{2})$');
    var txt = document.getElementById('<%= txt.ClientID %>');
    if (txt.value.match(reg) != null)
        return true;
    else
        return false;
}

Mark Up

<asp:LinkButton runat="server" ID="lnkSubmit" Text="Submit" 
                                 OnClientClick="return Validation();" />
<asp:TextBox ID="txt" runat="server"></asp:TextBox>

Upvotes: 0

SLaks
SLaks

Reputation: 887453

Your string literal is swallowing the \ escapes.

Instead, you should use a regex literal: /(1[0-2]|0[1-9]|\d)\/(20\d{2}|19\d{2}|0(?!0)\d|[1-9]\d)$/

Upvotes: 4

Related Questions