You must Sign In to post a response.

DateTime validation in Javascript

Hi guys,

i would like to validate the DateTime field in Javascript Please tell me the any suggestion...


regards,
Parshu


Comments

Author: Ashokkumar21 Jul 2008 Member Level: Gold   Points : 6

hi

yes we to validate the datetime functions in Javascript

i have write the function for compare the dates

/***********************************************
Function : DateCompare
Purpose : Validation For Date
Inputs : (date1Str,date2Str)Date
Output : (alert)String
Using : RUD_RATE
************************************************/

function DateCompare (date1Str, date2Str)
{
var date1 = new Date(date1Str.value);
var date2 = new Date(date2Str.value);
if (date1 > date2)
{
alert ("End Date is less then Start Date");
return false;
}
else
{
return true;
}
}

Author: Parashu(Swathimutyam)21 Jul 2008 Member Level: Gold   Points : 2

Ashok my question is how to Validate the DateTime Field for Example 05/05/2005 12:00:00 AM the entire field need to validate

Author: Dharmaraj Nagarajan21 Jul 2008 Member Level: Gold   Points : 6

Hi,




Author: mahalakshmi21 Jul 2008 Member Level: Gold   Points : 6

javascript:

var ctrl = document.getElementById('');

var v = ctrl.value;

if (v.replace(/^\\s+|\\s+$/, '').length != 0 && isNaN(Date.parse(v))) {
alert('Invalid value!');
ctrl.focus();
return false;
}

This javascript allows value to be empty but if value is not empty then it has to be a valid datetime string. Yor input string has no day, so you need some formatting before checking:

v = v.substr(1,2)+'/01/'+v.substr(4);

For your example it will return "11/01/2007 11:12:59" (here 01 as "dummy" day).

It is very unusually to have time part without day in the date part..

Author: mahalakshmi21 Jul 2008 Member Level: Gold   Points : 6

hi,


Author: LakshmiRameshCherukuri21 Jul 2008 Member Level: Silver   Points : 6


//Date Of Birth
var dob=document.getElementById('txtDateofBirth');
var doj=document.getElementById('txtDateofJoin');

if(dob.value=="")
{
alert('Date of Birth is Required');
return false;
}
if(!isValidDate(dob.value))
{
return false;
}
if(!checkBirthday())
{
return false;
}
function isValidDate(dateStr)
{
// Checks for the following valid date formats:
// MM/DD/YY MM/DD/YYYY MM-DD-YY MM-DD-YYYY
// Also separates date into month, day, and year variables

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

// To require a 4 digit year entry, use this line instead:
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null)
{
alert("Date is not in a valid format.")
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12)
{
// check month range
alert("Month must be between 1 and 12");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31)
{
alert("Month "+month+" doesn't have 31 days!")
return false
}
if (month == 2)
{ // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
}
}
return true; // date is valid
}

function checkBirthday()
{
var thetime=new Date();
var nday=thetime.getDay();
var nmonth=thetime.getMonth();
var ntoday=thetime.getDate();
var nyear=thetime.getYear();
if (nyear<=99)
{
nyear= "19"+nyear;
}

if ((nyear>99) && (nyear<2000))
nyear+=1900;
if(ntoday<10)
{
ntoday="0"+ntoday;
}
if(nmonth<10)
{
nmonth="0"+nmonth;
}

var currentDate=nmonth+"/"+ntoday+"/"+nyear;
var UserDate=document.getElementById('txtDateofBirth').value;
if(Date.parse(UserDate)>=Date.parse(currentDate))
{
alert('Invalid Date of Birth');
return false;
}
return true;

}

Author: Arunraam22 Jul 2008 Member Level: Silver   Points : 4

hi,

function validation(ctl3) {
var ctrl = document.getElementById(ctl3);
var v = ctrl.value;
if (v.replace(/^\\s+|\\s+$/, '').length != 0 && isNaN(Date.parse(v))) {
alert('Invalid Date Format!');
ctrl.value="";
ctrl.focus();
return false;
}
}


try this code

Author: Parashu(Swathimutyam)22 Jul 2008 Member Level: Gold   Points : 0

Thanks to All


Sign In to post your comments