SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Validating date

  1. #1
    SitePoint Enthusiast nkacharani's Avatar
    Join Date
    Oct 2010
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validating date

    Hello members,

    I have this code which is supposed to be testing the entered date by the user but it seem not to be working can someone please help me to check where I'm going wrong.



    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script language="javascript" type="text/javascript">
    function isDate(txtDate) {
    ****var objDate,* // date object initialized from the txtDate string
    ********mSeconds, // txtDate in milliseconds
    ********day,***** // day
    ********month,*** // month
    ********year;**** // year
    ****// date length should be 10 characters (no more no less)
    ****if (txtDate.length !== 10) {
    ********return false;
    ****}
    ****// third and sixth character should be '/'
    ****if (txtDate.substring(2, 3) !== '/' || txtDate.substring(5, 6) !== '/') {
    ********return false;
    ****}
    ****// extract month, day and year from the txtDate (expected format is mm/dd/yyyy)
    ****// subtraction will cast variables to integer implicitly (needed
    ****// for !== comparing)
    ****month = txtDate.substring(0, 2) - 1; // because months in JS start from 0
    ****day = txtDate.substring(3, 5) - 0;
    ****year = txtDate.substring(6, 10) - 0;
    ****// test year range
    ****if (year < 1000 || year > 3000) {
    ********return false;
    ****}
    ****// convert txtDate to milliseconds
    ****mSeconds = (new Date(year, month, day)).getTime();
    ****// initialize Date() object from calculated milliseconds
    ****objDate = new Date();
    ****objDate.setTime(mSeconds);
    ****// compare input date and parts from Date() object
    ****// if difference exists then date isn't valid
    ****if (objDate.getFullYear() !== year ||
    ********objDate.getMonth() !== month ||
    ********objDate.getDate() !== day) {
    ********return false;
    ****}
    ****// otherwise return true
    ****return true;
    }


    function checkDate(){
    ****// define date string to test
    ****var txtDate = document.getElementById('txtDate').value;
    ****// check date and print message
    ****if (isDate(txtDate)) {
    ********alert('OK');
    ****}
    ****else {
    ********alert('Invalid date format!');
    ****}
    }
    </script>
    </head>

    <body>
    <form id="form1" name="form1" method="post" action="" onsubmit="checkDate(textDate); return document.checkDate">
    <label for="date">date</label>
    <input type="text" name="txtDate" id="txtDate" />
    <label for="button"></label>
    <input type="submit" name="button" id="button" value="Submit" />
    </form>
    </body>
    </html>

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <!doctype html>
    <html lang= "en">
    <head>
    <meta charset= "utf-8">
    <title> Untitled Document</title>
    <script>
    function isDate(txtDate){
    	if(/^\d{2}\/\d{2}\/\d{4}$/.test(txtDate)){
    		var objDate, mSeconds, day, month, year;
    		txtDate= txtDate.split(/\D+/);
    		month= parseInt(txtDate[0], 10)- 1;
    		day= parseInt(txtDate[1], 10);
    		year= parseInt(txtDate[2], 10);
    		if(year < 1000 || year> 3000){
    			return false;
    		}
    		objDate= new Date(year, month, day);
    		if(objDate.getFullYear()=== year &&
    		objDate.getMonth()=== month &&
    		objDate.getDate()=== day){
    			return true;
    		}
    	}
    	return false;
    }
    function checkDate(){
    	var txtDate= document.getElementById('txtDate').value;
    	if(isDate(txtDate)) alert('OK');	 
    	else alert('Invalid date format!');
    	 
    }
    </script>
    </head>
    <body>
    <form id= "form1" onsubmit="return false;">
    <label for= "txtDate"> Input date as mm/dd/yyyy </label>
    <input type= "text"  id= "txtDate">
    <input type= "button"  value= "Submit" onclick="checkDate()">
    </form>
    </body>
    </html>

  3. #3
    SitePoint Enthusiast nkacharani's Avatar
    Join Date
    Oct 2010
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for this clarification..
    Last edited by nkacharani; May 29, 2011 at 12:24. Reason: got the solution


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
  •