Regular Expression to match dates

jQuery.validator.addMethod(
“numericUpCheck”,
function(value,element) {
//alert(“hai”);
var regex = /^([0-9]{4}\/\[0-9]{1,2}\/\[0-9]{1,2})?$/
if (element.value.search(regex) == -1) {
return false;
} else
return true;
},
“Please Enter Numeric Values.”
);

pl tell me regx value for date validation…if i gava above value na…corrct date format also cant accept…i wana this format yy/mm/dd pl help me

Try this.
var regex = /[1]{2}\/[0-9]{2}\/[0-9]{2}$/
And next time… Please proofread what you post. :slight_smile:


  1. 0-9 ↩︎

ya i got but im using this value /[2]{2}[0-9]{2}\/[0-9]{2}\/[0-9]{2}$/


  1. 0-9 ↩︎

  2. 0-9 ↩︎

In first post you wanted yy not yyyy (~;

i want yyyy/mm/dd

var regex = /[1]{2}\/[0-9]{2}\/[0-9]{2}$/

change to

var regex = /[2]{4}\/[0-9]{2}\/[0-9]{2}$/

The problem with this regex is that you will be able to enter something like 3333/22/66

RLM


  1. 0-9 ↩︎

  2. 0-9 ↩︎

I wrote this months ago, it’s from a previous post. Just ammended it to fit yyyy/mm/dd.

It doesn’t really limit the years, but does validate for months and days including leap years.

function isDate(input){ 
	return /^(((\\d{4})(\\/)(0[13578]|10|12)(\\/)(0[1-9]|[12][0-9]|3[01]))|(\\d{4})(\\/)((0[469]|11)(\\/)([0][1-9]|[12][0-9]|30))|((\\d{4})(\\/)(02)(\\/)(0[1-9]|1[0-9]|2[0-8]))|(([02468][048]00)(\\/)(02)(\\/)(29))|(([13579][26]00)(\\/)(02)(\\/)(29))|(([0-9][0-9][0][48])(\\/)(02)(\\/)(29))|(([0-9][0-9][2468][048])(\\/)(02)(\\/)(29))|(([0-9][0-9][13579][26])(\\/)(02)(\\/)(29)))$/.test(input);
}
console.log(isDate("1980/02/29")); //true
console.log(isDate("1981/02/29")); // false
console.log(isDate("2009/04/31")); // false
console.log(isDate("2009/03/31")); // true
console.log(isDate("2007/43/31")); // false
// the years aren't confined though.
console.log(isDate("1066/10/14")); // true
console.log(isDate("2553/10/30")); // true (in Thailand anyway:D)

RLM

Edit: Now accepts a 100 year range up to this year.

var formObj = (function(){

	var dateRx = new RegExp("^(((\\\\d{4})(\\\\/)(0[13578]|10|12)(\\\\/)(0[1-9]|[12][0-9]|3[01]))|(\\\\d{4})"+
	"(\\\\/)((0[469]|11)(\\\\/)([0][1-9]|[12][0-9]|30))|((\\\\d{4})(\\\\/)(02)(\\\\/)(0[1-9]|1[0-9]|2[0-8]))|"+
	"(([02468][048]00)(\\\\/)(02)(\\\\/)(29))|(([13579][26]00)(\\\\/)(02)(\\\\/)(29))|(([0-9][0-9][0][48])"+
	"(\\\\/)(02)(\\\\/)(29))|(([0-9][0-9][2468][048])(\\\\/)(02)(\\\\/)(29))|(([0-9][0-9][13579][26])(\\\\/)"+
	"(02)(\\\\/)(29)))$");
	
	var now = new Date(),
	yrNow = now.getFullYear(), yrThen = yrNow - 100;

	return {
		isDate : function (input){
				// match the first four digits and assign to yRx
				var yRx = parseInt(input.match(/^\\d{4}/));
				// if yrx is valid, and in a range of the last 100 years then validate further
				// returning true or false.
				if (yRx && yRx < yrNow && yRx > yrThen){ return dateRx.test(input);}
				// Otherwise the year has failed outright so return false
				return false;
				}
	};
}());

Quick Tests

console.log(formObj.isDate("1980/02/29")); // true
console.log(formObj.isDate("1981/02/29")); // false
console.log(formObj.isDate("2009/04/31")); // false
console.log(formObj.isDate("2009/03/31")); // true
console.log(formObj.isDate("2007/43/31")); // false
console.log(formObj.isDate("1066/10/14")); // false
console.log(formObj.isDate("2200/09/12")); // false

RLM