Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: Date Validation

  1. #1
    New Coder
    Join Date
    Jan 2004
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Date Validation

    Hi
    i am new to Java script.hv do i check that user enters correct date format. the format for accepting is 02/02/04.
    if anyhelp i would appreciate

    thanks

  • #2
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Hello -

    Hopefully this will work for you:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function dateValid(field)
    {
    	var str=document.getElementById(field).value; 
    	var stat=0;
    	checkField();
    	
    	function checkField()
    	{
    		for(i=0;i<str.length;i++)
    		{
    			if(i==2||i==5)
    			{
    				var chk=str.charAt(i);
    				
    				if(chk!='/')
    				{
    					stat=1;
    				}
    			}
    
    			else
    			{
    				var chk=str.charAt(i);
    				
    				if(isNaN(chk))
    				{
    					stat=1;
    				}
    			}
    		}
    	}
    		
    	if(stat==1)
    	{
    		alert("You Must Enter The Date In This Format: mm/dd/yy");
    	}
    
    }
    </script>
    </head>
    <body>
    <form id="myForm">
    <input type="text" id="fDate"><input type="button" onclick="dateValid('fDate')" value="Submit">
    </body>
    </html>
    Hope That Helps,
    Basscyst
    Last edited by Basscyst; 02-02-2004 at 11:48 PM.
    Helping to build a bigger box. - Adam Matthews

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    There's always good ol' regular expressions.

    Code:
    function checkdateformat(userinput){
    var dateformat = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
    return dateformat.test(userinput) //returns true or false depending on userinput
    }
    http://www.javascriptkit.com/javatutors/re4.shtml

  • #4
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Yeah I'm still working on those - Dang that makes it much smaller.

    Basscyst
    Helping to build a bigger box. - Adam Matthews

  • #5
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    I haven't figured them out yet either, but I remembered reading that at javascriptkit.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,968
    Thanks
    0
    Thanked 236 Times in 233 Posts
    That would only check the format but not the validity of the date. i.e. 02/31/2004


  •  

    Posting Permissions

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