Syed Salman Raza Zaidi
Syed Salman Raza Zaidi

Reputation: 2192

Validating Data using JS Regex

I am using javascript regex for validating user entered date in format mm/yyyy. I have made regex for this and its working fine, but when I enter ab/abcd its still validating it.

This is my JS and HTML code

function validate() {
    var name = document.getElementById("name").value;
    var pattern = /\d|\/|\d{4}/;        
    if (pattern.test(name)) {
        alert(name +" has alphanumeric value");
        return true;
    } else {
        alert("Name is not valid.Please input alphanumeric value!");
        return false;
    }
}
Date: <input type="text" name="name" id="name" />
<input type="submit" value="Check" onclick="validate();"/>

How can I validate the entered date in mm/yyy format, I am using Jquery DatePicker which allows to select date in this format, and the datepicker also allows manual input. like this Date Picker Is there any alternate suggestion for this?

Upvotes: 0

Views: 39

Answers (1)

Kyo
Kyo

Reputation: 964

Updated:

Try the pattern below instead:

var pattern = /^\d{2}\/\d{4}$/;    

See the working code at:

JSFiddle

Another fiddle for extracting the month:

JsFiddle2

Upvotes: 1

Related Questions