View Full Version : date format function
nikko50
10-05-2004, 10:40 PM
Hello all.
Looking for a function that validates date fields . Field must be formatted as such... mm/dd/yyyy. Anybody have a function available or provide a link for one. Thanks
Tracy
Willy Duitt
10-05-2004, 10:51 PM
You need to be more specific...
Validate what... that it is a valid date, that it is either a previous date or one in the future or what...
.....Willy
sad69
10-05-2004, 10:52 PM
/*
Validates a string as a date in mm/dd/yyyy format.
Returns true if valid, false otherwise.
*/
function validate_date(s_date) {
var date_params = s_date.split('/'); //0:mm, 1:dd, 2:yyyy
if(date_params.length != 3 || date_params[0].length != 2 || date_params[1].length != 2 || date_params[2].length != 4)
return false;
var try_date = new Date(date_params[2], date_params[0]-1, date_params[1]); //requires y,m,d
if(date_params[0]-1 != try_date.getMonth() || date_params[1] != try_date.getDate() || date_params[2] != try_date.getYear())
return false;
return true;
}
Sadiq.
nikko50
10-05-2004, 11:34 PM
Hi guys. Thanks for the quick response. How would I use that code sadiq sent in the below code.
<head>
<script>
/*
Validates a string as a date in mm/dd/yyyy format.
Returns true if valid, false otherwise.
*/
function validate_date(s_date) {
var date_params = s_date.split('/'); //0:mm, 1:dd, 2:yyyy
if(date_params.length != 3 || date_params[0].length != 2 || date_params[1].length != 2 || date_params[2].length != 4)
return false;
var try_date = new Date(date_params[2], date_params[0]-1, date_params[1]); //requires y,m,d
if(date_params[0]-1 != try_date.getMonth() || date_params[1] != try_date.getDate() || date_params[2] != try_date.getYear())
return false;
return true;
}
</script>
<title>New Page 1</title>
</head>
<body>
<form method="POST" action="--WEBBOT-SELF--">
<p><input type="text" name="date" size="20"></p>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
</body>
sad69
10-05-2004, 11:49 PM
At the time of posting your initial question, how were you planning to use a validating function if someone had posted one for you?
There are a couple ways:
-the function can be called when the user finishes typing in the box
-the function can be called when the user presses the submit button
What do you want to happen if the function returns false (ie. the user incorrectly entered in the date...)?
There are many ways that this can be done, which one gets implemented is up to you.
It seems like you're new to Javascript, so you may want to read up on event handlers, such as onchange and onsubmit using Google or something.
Sadiq.
nikko50
10-06-2004, 12:26 AM
Yes I'm new. I would like it with an onSubmit.
Tracy
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.