SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    secure webapps for all Aleksejs's Avatar
    Join Date
    Apr 2008
    Location
    Riga, Latvia
    Posts
    755
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this.
    var regex = /^[0-9]{2}\/[0-9]{2}\/[0-9]{2}$/
    And next time... Please proofread what you post.

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Aleksejs View Post
    Try this.
    var regex = /^[0-9]{2}\/[0-9]{2}\/[0-9]{2}$/
    And next time... Please proofread what you post.
    ya i got but im using this value /^[0-9]{2}[0-9]{2}\/[0-9]{2}\/[0-9]{2}$/

  4. #4
    secure webapps for all Aleksejs's Avatar
    Join Date
    Apr 2008
    Location
    Riga, Latvia
    Posts
    755
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In first post you wanted yy not yyyy (~;

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Aleksejs View Post
    In first post you wanted yy not yyyy (~;
    i want yyyy/mm/dd

  6. #6
    SitePoint Addict
    Join Date
    Nov 2008
    Location
    Thailand
    Posts
    329
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    var regex = /^[0-9]{2}\/[0-9]{2}\/[0-9]{2}$/

    change to

    var regex = /^[0-9]{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

  7. #7
    SitePoint Addict
    Join Date
    Nov 2008
    Location
    Thailand
    Posts
    329
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

    Code JavaScript:
    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

  8. #8
    SitePoint Addict
    Join Date
    Nov 2008
    Location
    Thailand
    Posts
    329
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Edit: Now accepts a 100 year range up to this year.

    Code JavaScript:
    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
    Code:
    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


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •