Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

validate Date Format dd/mm/yyyy

Posted on 2004-11-11
7
Medium Priority
?
6,622 Views
Last Modified: 2008-02-01
Hi
I have found the follwoing script which works great, however i would like to adapt it so that this can be applied to two date fields in my form

<script language=javascript>
/**********************************************************************/
/*Function name :isDigit(theDigit) */
/*Usage of this function :test for an digit */
/*Input parameter required:thedata=string for test whether is digit */
/*Return value :if is digit,return true */
/* else return false */
/**********************************************************************/
function isDigit(theDigit)
{
var digitArray = new Array('0','1','2','3','4','5','6','7','8','9'),j;

for (j = 0; j < digitArray.length; j++)
{if (theDigit == digitArray[j])
return true
}
return false

}
/*************************************************************************/
/*Function name :isPositiveInteger(theString) */
/*Usage of this function :test for an +ve integer */
/*Input parameter required:thedata=string for test whether is +ve integer*/
/*Return value :if is +ve integer,return true */
/* else return false */
/*function require :isDigit */
/*************************************************************************/
function isPositiveInteger(theString)
{
var theData = new String(theString)

if (!isDigit(theData.charAt(0)))
if (!(theData.charAt(0)== '+'))
return false

for (var i = 1; i < theData.length; i++)
if (!isDigit(theData.charAt(i)))
return false
return true
}
/**********************************************************************/
/*Function name :isDate(s,f) */
/*Usage of this function :To check s is a valid format */
/*Input parameter required:s=input string */
/* f=input string format */
/* =1,in mm/dd/yyyy format */
/* else in dd/mm/yyyy */
/*Return value :if is a valid date return 1 */
/* else return 0 */
/*Function required :isPositiveInteger() */
/**********************************************************************/
function isDate(s,f)
{var a1=s.split("/");
var a2=s.split("-");
var e=true;
if ((a1.length!=3) && (a2.length!=3))
{
e=false;
}
else
{if (a1.length==3)
var na=a1;
if (a2.length==3)
var na=a2;
if (isPositiveInteger(na[0]) && isPositiveInteger(na[1]) && isPositiveInteger(na[2]))
{ if (f==1)
{var d=na[1],m=na[0];
}
else
{var d=na[0],m=na[1];
}
var y=na[2];
if (((e) && (y<1000)||y.length>4))
e=false
if (e)
{
v=new Date(m+"/"+d+"/"+y);
if (v.getMonth()!=m-1)
e=false;
}
}
else
{
e=false;
}
}
return e
}
function checkDate(v)
{
var s=v.a.value;
if (isDate(s,0)) //dd/mm/yyyy format
return true;
else
alert("The inputted date value is not valid!");
return false;
}

function checkDate(v)
{
var s=v.a.value;
if (isDate(s,0)) //dd/mm/yyyy format
return true;
else
alert("The inputted date value is not valid!");
return false;
}

</script>

<form name="frmParam" id="frmParam" method="post" action="ReportRes2.asp" onSubmit="return checkDate(this)">
         
<input name="txtStartDate" type="text" id="a" />
       

The second text field is <input name="txtEndDate" type="text" id="txtEndDate" />.  Any help much appreciated as i have spent all day trying to figure this out
Ta
0
Comment
Question by:jkharodia
5 Comments
 
LVL 7

Expert Comment

by:searlas
ID: 12556080
Change:
function checkDate(v)
{
var s=v.a.value;
if (isDate(s,0)) //dd/mm/yyyy format
return true;
else
alert("The inputted date value is not valid!");
return false;
}

to:
function checkDate(field)
{
var s=field.value;
if (isDate(s,0)) //dd/mm/yyyy format
return true;
else
alert("The inputted date value is not valid!");
return false;
}

And change
 onSubmit="return checkDate(this)"

to:
 onSubmit="return (checkDate(this.a) && checkDate(this.txtEndDate)"
0
 
LVL 7

Expert Comment

by:searlas
ID: 12556091
Missing ")"

 onSubmit="return (checkDate(this.a) && checkDate(this.txtEndDate))"
0
 
LVL 33

Accepted Solution

by:
sajuks earned 672 total points
ID: 12556096
<script language=javascript>
/**********************************************************************/
/*Function name :isDigit(theDigit) */
/*Usage of this function :test for an digit */
/*Input parameter required:thedata=string for test whether is digit */
/*Return value :if is digit,return true */
/* else return false */
/**********************************************************************/
function isDigit(theDigit)
{
var digitArray = new Array('0','1','2','3','4','5','6','7','8','9'),j;

for (j = 0; j < digitArray.length; j++)
{if (theDigit == digitArray[j])
return true
}
return false

}
/*************************************************************************/
/*Function name :isPositiveInteger(theString) */
/*Usage of this function :test for an +ve integer */
/*Input parameter required:thedata=string for test whether is +ve integer*/
/*Return value :if is +ve integer,return true */
/* else return false */
/*function require :isDigit */
/*************************************************************************/
function isPositiveInteger(theString)
{
var theData = new String(theString)

if (!isDigit(theData.charAt(0)))
if (!(theData.charAt(0)== '+'))
return false

for (var i = 1; i < theData.length; i++)
if (!isDigit(theData.charAt(i)))
return false
return true
}
/**********************************************************************/
/*Function name :isDate(s,f) */
/*Usage of this function :To check s is a valid format */
/*Input parameter required:s=input string */
/* f=input string format */
/* =1,in mm/dd/yyyy format */
/* else in dd/mm/yyyy */
/*Return value :if is a valid date return 1 */
/* else return 0 */
/*Function required :isPositiveInteger() */
/**********************************************************************/
function isDate(s,f)
{var a1=s.split("/");
var a2=s.split("-");
var e=true;
if ((a1.length!=3) && (a2.length!=3))
{
e=false;
}
else
{if (a1.length==3)
var na=a1;
if (a2.length==3)
var na=a2;
if (isPositiveInteger(na[0]) && isPositiveInteger(na[1]) && isPositiveInteger(na[2]))
{ if (f==1)
{var d=na[1],m=na[0];
}
else
{var d=na[0],m=na[1];
}
var y=na[2];
if (((e) && (y<1000)||y.length>4))
e=false
if (e)
{
v=new Date(m+"/"+d+"/"+y);
if (v.getMonth()!=m-1)
e=false;
}
}
else
{
e=false;
}
}
return e
}
function checkDate(v)
{
var s=v.a.value;
var t=v.txtEndDate.value;
if (isDate(s,0)) //dd/mm/yyyy format
{
    if (isDate(t,0)) //dd/mm/yyyy format
    {

    return true;
    }
    else
    {
    v.txtEndDate.focus()
    alert("The inputted date value is not valid!");
    return false;
    }

}
else
{
    v.a.focus()
alert("The inputted date value is not valid!");
return false;
}
 }


</script>

<form name="frmParam" id="frmParam" method="post" action="ReportRes2.asp" onSubmit="return checkDate(this)">
         
<input name="txtStartDate" type="text" id="a" />


<input name="txtEndDate" type="text" id="txtEndDate" />
<INPUT TYPE="submit">        
</form>
0
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 664 total points
ID: 12556209
Here my proposal:


<html>
<head>
<script>

function checkForm(theForm){
  var sDate, eDate;
  if((sDate = getDate(theForm.txtStartDate)) &&
     (eDate = getDate(theForm.txtEndDate))){
     if(sDate>eDate){
       alert("Start Date is past the End Date.")
       theForm.txtStartDate.select();
       theForm.txtStartDate.focus();
       return false;
     } else {
       return true;
     }
  } else {
    return false;
  }
}

function getDate(theField){
  var dPart = theField.value.split("/");
  if(dPart.length==3){
    var tDate = new Date(dPart[2], dPart[1]-1, dPart[0]);
    if(isNaN(tDate) || tDate.getFullYear()!=dPart[2] ||
       tDate.getMonth()!=dPart[1]-1 || tDate.getDate()!=dPart[0]){
       alert("Enter Date in this format: DD/MM/YYYY");
       theField.select();
       theField.focus();
       return false;
     }
     if(tDate.getFullYear()<1000||tDate.getFullYear()>3000){
       alert("The Year is out of range: "+tDate.getFullYear());
       theField.select();
       theField.focus();
       return false;
     }
     return tDate;
  } else {
    alert("Enter Date in this format: DD/MM/YYYY");
    theField.select();
    theField.focus();
    return false;
  }
}

</script>
</head>
<body>
<form name="frmParam" id="frmParam" method="post" action="ReportRes2.asp" onSubmit="return checkForm(this)">
     
<input name="txtStartDate" type="text" id="a" />
<input name="txtEndDate" type="text" id="txtEndDate" />
<input type=submit>
</form>
</body>
</html>



0
 
LVL 31

Assisted Solution

by:GwynforWeb
GwynforWeb earned 664 total points
ID: 12556651
my try

<script>
function validDate(obj){
 date=obj.value
 test1=(/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(date))
 date=date.split('/')
 d=new Date(date[2],date[1]-1,date[0])
 test2=(1*date[0]==d.getDate() && 1*date[1]==(d.getMonth()+1) && 1*date[2]==d.getYear())
 if (test1 && test2) return true
 alert("Invalid date")
 obj.select();
 obj.focus()
 return false
}
</script>

<form>
  <p>Date1: <input type="text" name="startDate" size="9" maxLength="10" onChange="validDate(this)"> </p>
  <p>Date2: <input type="text" name="startDate" size="9" maxLength="10" onChange="validDate(this)"> </p>
</form><p>

or

<form action="http://google.com" onsubmit='return (validDate(d1) && validDate(d2))')>
  <p>Date1: <input type="text" name="d1" size="9" maxLength="10"> </p>
  <p>Date2: <input type="text" name="d2" size="9" maxLength="10"></p>
</form>
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question