SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    validating date w/ javascript

    Im trying to validate my date textbox on my form to only allow mm/dd/yy. If the user enters anything else in I want it to display an error message and focus on the date textbox. Ive looked at many sample scripts with no luck. Anyone out there done this before, and possibly still have the code? Thanks...

  2. #2
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is an example of validating a date that I put together for someone that wanted to check if the person was 18 years or more. Hope it helps.
    <html>
    <head>
    <title>Date Form Test</title>
    <SCRIPT LANGUAGE="JavaScript">
    // Get current date
    curdate=new Date()
    curmonth=curdate.getMonth()+1
    curday=curdate.getDate()
    curyear=curdate.getFullYear()
    if(curday<10)
    {curday="0"+curday }

    if(curmonth<10)
    {curmonth="0"+curmonth}
    cdate=""+curyear+curmonth+curday
    function chkdate()
    {
    var cday=""
    var cmonth=""
    var mdaysa=new Array(31,28,31,30,31,30,31,31,30,31,30,31)
    var mdaysb=new Array(31,29,31,30,31,30,31,31,30,31,30,31) // for leap year
    // grab date from text box
    var bdate=document.myform.tdate.value
    // separate out day, month, year and slashes and convert string numbers to numeric numbers
    bday=parseFloat(bdate.substr(3,2))

    bsl1=bdate.substr(2,1)
    bmonth=parseFloat(bdate.substr(0,2))
    bsl2=bdate.substr(5,1)
    byear=parseFloat(bdate.substr(6,4))
    if(bdate.length<10||byear==0) // Insure full date is entered and year is not zeroes
    {
    alert("Date must be entered in mm/dd/yyyy format\rand the year cannot equal zero")
    document.myform.tdate.focus()
    }
    else
    {
    // check for is-Not-a-Number and slashes
    if(isNaN(bday) || isNaN(bmonth) || isNaN(byear) || bsl1!="/" || bsl2!="/")
    {
    alert("Date must be entered in mm/dd/yyyy format")
    document.myform.tdate.focus()
    }
    else
    {
    if(bmonth<1 || bmonth>12) // check for valid month
    {
    alert("The month is incorrect")
    document.myform.tdate.focus()
    }
    else
    {
    remain=byear % 4
    if(remain!=0) // if not leap year
    {
    if(bday>mdaysa[bmonth-1]||bday==0) // check for valid day
    {
    alert("The day is incorrect")
    document.myform.tdate.focus()
    }
    else
    {
    birthdate(byear,bmonth,bday)
    }
    }
    else
    {
    if(bday>mdaysb[bmonth-1]||bday==0) // check for valid day - leap year
    {
    alert("The day is incorrect")
    document.myform.tdate.focus()
    }
    else
    {
    birthdate(byear,bmonth,bday)
    }
    }
    }
    }
    }
    }
    function birthdate(year,month,day)
    {
    cyear=year+18
    if(cyear<curyear)
    {alert("You are at least 18 years old!")}
    else
    {
    if(cyear==curyear&&month<=curmonth&&day<=curday)
    {alert("You are at least 18 years old!")}
    else
    {alert("You are not at least 18 years old!")}
    }
    }
    </SCRIPT>
    </head>
    <body bgcolor="lightgreen" onLoad="document.myform.tdate.focus()">
    <CENTER>
    <FORM NAME="myform">
    Enter Your Birth Date: <INPUT TYPE="text" NAME="tdate" SIZE="10" MAXLENGTH="10">
    <BR>
    <INPUT TYPE="button" NAME="but1" VALUE="Check Date" onClick="chkdate()">
    <BR><BR>
    <FONT SIZE="-1" COLOR="red">Please use date format of: mm/dd/yyyy.</FONT>
    </FORM>
    </CENTER>
    </body>
    </html>

  3. #3
    SitePoint Member doggi's Avatar
    Join Date
    Oct 2001
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is a very simple check. The script converts the given date to a Date object. Then you format this date back to what it should be, and then you compare it with the original. If the conversion between string -> date -> string was the same, then the original date was valid. Otherwise this would not work and the test string would be something completely different.

    Code:
    function check(mydate)
    {
    	var objdate = new Date(mydate);
    	var d = objdate.getDate();
    	var m = objdate.getMonth()+1;
    	var y = objdate.getYear();
    	var test = m + '/' + d + '/' + y;
    	if (test==mydate) alert('valid');
    	else alert('not valid');
    
    }


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
  •