SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: date validation

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question date validation

    halo there..i got two date field..tat is start date and end date..and my date format from a calendar is 2007-11-20..when i try to do a validation using the code below,but it cant work...can some one help me plz? thanx alot..plz...help..thanx
    Code:
    if (new Date(document.frm_addpromotion.enddate.value) < new Date(document.frm_addpromotion.startdate.value))
           {
            alert("End date should be later than the start date.");
              document.frm_addpromotion.enddate.focus();
              return false;
           }

  2. #2
    Motivated Procrastinator seriocomic's Avatar
    Join Date
    Jan 2003
    Location
    Outside the bubble
    Posts
    1,275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'll probably get a better response than mine, but what immediately jumps out at me is that the value returned is not a true numeric value.

    By your example you are saying that the value is 2007-11-20. You are expecting javascript to recognize the mathematical difference between two dates. It is limited into recognizing the mathematical difference between numbers only.

    Enter the JavaScript experts to tell you how that can be done...

  3. #3
    Motivated Procrastinator seriocomic's Avatar
    Join Date
    Jan 2003
    Location
    Outside the bubble
    Posts
    1,275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something along these lines

    Code JavaScript:
    function DateCheck() {
    	if (document.frm_addpromotion.enddate.value != "") {
    		var start = document.frm_addpromotion.startdate.value
    		var end = document.frm_addpromotion.enddate.value
    		starts = new Array();
    		starts = date.split("-")
    		if (starts[1].length == 1) starts[1] = "0" + starts[1]
    		if (starts[2].length == 1) starts[2] = "0" + starts[2]
    		start = starts.join("")
    		ends = new Array();
    		ends = end.split("-");
    		if (ends[1].length == 1) ends[1] = "0" + ends[1]
    		if (ends[2].length == 1) ends[2] = "0" + ends[2]
    		end = ends.join("")
    		if (start >= end) {
    		   alert("The end date must be greater than the start date.");
    		   document.frm_addpromotion.startdate.select();
    		   return false;
    		}
    	}
    }


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
  •