I am doing a project in JSP in which I want to do the client side validation using Javascript for the date fields
"from_date" and "to_date".
I am using Six combo boxes for getting dates ( 3 for 'from_date'[mm,dd,yy] and 3 for 'to_date' [mm,dd,yy] ).
I want the 'from_date' to be smaller than the 'to_date'.
Also I have to take care of leap year and date validation.
Can you help me out in finding some code for it.
Can you send some source code or refer some websites for javascript resource code
-
javascript code for JSP *URGENT* (4 messages)
- Posted by: VIJAY KHANNA
- Posted on: February 21 2001 06:44 EST
Threaded Messages (4)
- javascript code for JSP *URGENT* by Sudha Durairaj on February 21 2001 11:38 EST
- javascript date chack by Niels Ull Harremoes on February 26 2001 05:43 EST
- javascript code for JSP *URGENT* by Swati Chandna on February 26 2001 15:29 EST
- javascript code for JSP *URGENT* by srinivas rao on March 04 2005 03:44 EST
-
javascript code for JSP *URGENT*[ Go to top ]
- Posted by: Sudha Durairaj
- Posted on: February 21 2001 11:38 EST
- in response to VIJAY KHANNA
Hi,
In javascript its difficult to do data validation unless u find some convention of knowing what type of data the element is going to hold.I have done the same kind of validation in my proj. Its same fom & to date validations. for this u shd know which element is from date and which one is to date. I have averted this by foolowing a naming convention for my elements of the form. Its like this. For ex:
txt4FromDate - holds from dates
txt5ToDate - hold To dates
The "txt" means its going to hold a text value
and "4" means From date Type of data. "5" means To date Type of data. The function isDate(dateString) checks whether it is a valid date or not and isGreater(txt4FromDate,txt5ToDate) compares to dates.
I think this will be useful for u.
with best wishes
Sudha
// To verify whether a given date string is of date format or not. Inputs : DateString datestr
// Returns : Error Message
function isDate(datestr)
{
var passed="";
// var datePat2 = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
var datePat2 = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
var matchArray2 = datestr.match(datePat2);
if (matchArray2 == null)
{
passed += " should be of form MM/DD/YYYY ";
return passed;
}
month = matchArray2[1];
day = matchArray2[3];
year = matchArray2[4];
if (month < 1 || month > 12)
{
passed += " month must be between 1 and 12 ";
return passed;
}
if (day < 1 ||day > 31)
{
passed += " day must be between 1 and 31 ";
return passed;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31)
{
passed += " " + month + " doesn't have 31days ";
return passed;
}
if (month == 2)
{
var isleap = (year % 4== 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap))
{
passed += "February " + year + " doesn't have " + day + " days";
return passed;
}
}
return passed;
}
// To validate two date strings : Inputs : DateString FromDate, DateString Todate
// Returns Boolean ; Checks if todate is greater than From Date - Note :- No equality is checked
function isGreater(datestr,todate) // checks if datestr is grtr than todate
{
var dd;
var dd1
var mm;
var yyyy;
var yyyy1;
var d=todate;
// var datePat0 = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
var datePat0 = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // For ToDate
var matchArray0 = todate.match(datePat0);
mm = matchArray0[1];
dd = matchArray0[3];
yyyy = matchArray0[4];
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/; // For From Date
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
var matchArray = datestr.match(datePat);
var mm1 = matchArray[1];
dd1 = matchArray[3];
yyyy1 = matchArray[4];
/* if (yyyy1.length<=2)
{
dtObj = new Date(yyyy1,10,10);
yyyy1=dtObj.getFullYear();
}
if (yyyy.length<=2)
{
dtObj = new Date(yyyy,10,10);
yyyy=dtObj.getFullYear();
}
*/
if (yyyy1 < yyyy)
{
return false;
}
if ((mm1.length == 1) && (mm1 < 10)) mm1=0+mm1;
if ((mm.length == 1) && (mm < 10)) mm= 0+mm;
if (yyyy1 == yyyy && mm1 < mm)
{
return false;
}
if (dd.length == 1)
dd = "0"+dd;
if ((yyyy1 == yyyy) && (mm1 == mm) && (dd1 < dd))
{
return false;
}
return true;
}
-
javascript date chack[ Go to top ]
- Posted by: Niels Ull Harremoes
- Posted on: February 26 2001 05:43 EST
- in response to VIJAY KHANNA
The simplest solution to check for all the leap year stuff is to use the javascript Date class.
Here's some sample code we have used (copy/pasted, the indentation sucks).
var STDDATEFORMAT="YYYYMMDD";
var twoDigitYearCutOff = 50;
// Year 00 - 49 are converted to 2000-2049, Year 50-99
// are converted to 1950 - 1999
function IsDate(field)
{
if (field.value == "") return true;
var Numbers=/^\D*(\d+)\D+(\d+)\D+(\d+)\D*$/.exec(field.value);
if (Numbers == null) return false;
var d_ix, m_ix, y_ix;
if ( /D.*M.*Y.*/.test(STDDATEFORMAT) ) { d_ix=1; m_ix=2; y_ix=3; }
else if ( /M.*D.*Y.*/.test(STDDATEFORMAT) ) { m_ix=1; d_ix=2; y_ix=3; }
else if ( /Y.*M.*D.*/.test(STDDATEFORMAT) ) { y_ix=1; m_ix=2; d_ix=3; }
else if ( /Y.*D.*M.*/.test(STDDATEFORMAT) ) { y_ix=1; d_ix=2; m_ix=3; }
else if ( /D.*Y.*M.*/.test(STDDATEFORMAT) ) { d_ix=1; y_ix=2; m_ix=3; }
else { m_ix=1; y_ix=2; d_ix=3; }
var d=parseInt(Numbers[d_ix], 10);
var m=parseInt(Numbers[m_ix], 10);
var y=parseInt(Numbers[y_ix], 10);
if (y < 100) y += (y < 50) ? 2000 : 1900;
var checkdate=new Date(y, m-1, d);
if (
(checkdate.getMonth() != m-1) ||
(checkdate.getDate() != d) ||
( checkdate.getYear() != y ))
return false;
if (y > 9999) return false;
field.value=( (d_ix == 1) ? d : (m_ix == 1) ? m : y) + '-' +
( (d_ix == 2) ? d : (m_ix == 2) ? m : y) + '-' +
( (d_ix == 3) ? d : (m_ix == 3) ? m : y);
return true;
}
-
javascript code for JSP *URGENT*[ Go to top ]
- Posted by: Swati Chandna
- Posted on: February 26 2001 15:29 EST
- in response to VIJAY KHANNA
Here is the code which accepts two parameters , Start date and End Date and will return true if start date is before the end date...
I know this works if the dates passed are in the mm/dd/yyyy format, Should work for yyyy/mm/dd. I'm not sure of the other formats.
// compare Date returns true if the end date is greater than start date
function compareDate(sStartDate, sEndDate) {
dtStartDate = new Date(sStartDate);
dtEndDate = new Date(sEndDate);
var bReturn = true;
if (dtStartDate > dtEndDate) {
bReturn = false;
}
else {
bReturn = true;
}
return bReturn;
}
Note - Also there's a very good code for the date checking at the following code, u can as it is use the code on this site ---
http://javascript.internet.com/ or http://javascript.internet.com/new/index.html
Chk the sample code on "FormatDate". It's really cool.
-
javascript code for JSP *URGENT*[ Go to top ]
- Posted by: srinivas rao
- Posted on: March 04 2005 03:44 EST
- in response to VIJAY KHANNA
Hi
i am having a problem regarding date validation in jsp
my problem is
If is give invalid start date and end date like "14/02/2005 ----- MM/dd/yyyy "it is supporting if ending date is less than the starting date i need solution