...

View Full Version : Validating specific date format



a1jit
05-02-2004, 09:40 AM
Does anyone have any validation code that validates date according to dd/mmm/yyyy or dd-mmm-yyyy format.

I tried searching on google and also yahoo but all the codes basically validates date based on dd/mm/yy or mm/dd/yy.

If there is any useful resource or sample code, please post it.

Thank you very much in advance

liorean
05-02-2004, 01:09 PM
This doesn't match your date format, but it explains pretty well how you build it, and you should be able to reverse the order easily: RegExp - validate date.

Philip M
05-02-2004, 06:18 PM
I think a1jit wants to validate dates in the form
23-MAR-2004 or 23/AUG/2004

I am sure that this can be done, but why does he want to have his dates entered in this format to start with? An explanation of exactly what it is we are trying to achieve would help.

Another "Thank you in advance" first-time-visitor! Yuk! Puke!

Garadon
05-03-2004, 08:23 AM
oh lol I thought he just wanted year as 4 digits and simply put that 3rd m by mistake

the 6th edit field in the 2nd form
http://www.the-hive.dk/~donp/?f=6#validator

a1jit
05-03-2004, 10:47 AM
Yeah! ur right . i want the date to be validated in this format (23-MAR-2004 or 23/AUG/2004)

Actually im doing this for the users. They want the date to be entered in this format. So, i need to validate before i can actually store it in the database.

Help pleASE!!!

glenngv
05-03-2004, 12:02 PM
This code may not look elegant because it does not use regex. But the advantage of it over regex is it validates not only the format but also the validity of the date.


<script type="tex/javascript">
var arr = [];
arr['JAN'] = 0;
arr['FEB'] = 1;
arr['MAR'] = 2;
arr['APR'] = 3;
arr['MAY'] = 4;
arr['JUN'] = 5;
arr['JUL'] = 6;
arr['AUG'] = 7;
arr['SEP'] = 8;
arr['OCT'] = 9;
arr['NOV'] = 10;
arr['DEC'] = 11;

function isValidDate(strDate){
var arrDate = strDate.split('-');
if (arrDate.length==3 && strDate.length==11){
var d = new Date(arrDate[2], arr[arrDate[1]], arrDate[0]);
if (!isNaN(d) && d.getFullYear()==arrDate[2] && d.getMonth()==arr[arrDate[1]] && d.getDate()==arrDate[0]){
return true;
}
}
return false;
}
alert(isValidDate('31-JAN-2004')); //true
alert(isValidDate('29-FEB-2004')); //true
alert(isValidDate('30-FEB-2004')); //false
alert(isValidDate('31-APR-2004')); //false
</script>

Philip M
05-03-2004, 12:52 PM
This allows only for a hyphen as the delimiter.
So to allow for / as well:-

function isValidDate(strDate){
strDate.replace (/\//g, "-"); // replaces / by -
var arrDate = strDate.split('-');

and in case they enter spaces as well:-

strDate.replace (/\s/g,""); // deletes all spaces

My experience is that if the user can conceivably and by whatever improbable contortion fill in a form incorrectly, they will!

a1jit
05-03-2004, 01:23 PM
Thankx a lot for the help. I have a problem executiing the code. i have added a text field in your code. But i dont know why its not checking for the date validity. Can you pleaseeee show me how to link the javascript with a text field so that when user click the submit button, it checks for the date validity..thank you very very much!

Philip M
05-03-2004, 07:39 PM
Is this what you mean? That is, how to call the function when the submit button is clicked.

<FORM action="http://yourdomain.com/cgi-bin/formmail.pl"
method="POST" name="TheNameOfTheForm" onSubmit="return isValidDate(this.Submitbutton)">

<input type="submit" name="Submitbutton" value="Submit This Form">

</FORM>

But as it stands the function isValidDate() rejects inavlid entries but does not offer the opportunity for the user to re-enter the date correctly. You need to add something like

fieldname = ""; //erase the field
fieldname.focus(); // and reset the focus
return false;

a1jit
05-03-2004, 07:50 PM
Actually, im trying to modify the code pasted by glenngv. His code basically validates date based on the default date value. I want his code to validate date entered in a text field, not using default value. it means that i want his code to function this way

Basically user has a text field and also a submit button. so, user key in a date and clicks the submit button. After the submit button is clicked, the date is validated based on the javascript code pasted by glenngv.

If the date in not a valid date(means that it doesnt match the format[dd-mmm-yyyy]), then it says invalid date. If the date is a valid date, it submits the page.

a1jit
05-03-2004, 08:12 PM
I Managed to get the code running. This is how the code looks like

-------------------------------------------------------------------------
<html>
<head>
<title>Untitled Document</title>


<script type="text/javascript">


function isValidDate(strDate){

var str=document.getElementById(strDate).value;

var arr = [];
arr['JAN'] = 0;
arr['FEB'] = 1;
arr['MAR'] = 2;
arr['APR'] = 3;
arr['MAY'] = 4;
arr['JUN'] = 5;
arr['JUL'] = 6;
arr['AUG'] = 7;
arr['SEP'] = 8;
arr['OCT'] = 9;
arr['NOV'] = 10;
arr['DEC'] = 11;
var arrDate = str.split('-');
if (arrDate.length==3 && str.length==11){
var d = new Date(arrDate[2], arr[arrDate[1]], arrDate[0]);
if (!isNaN(d) && d.getFullYear()==arrDate[2] && d.getMonth()==arr[arrDate[1]] && d.getDate()==arrDate[0]){
alert("its valid");
return true;
}
}
alert("Not a valid date");
return false;
}

</script>

</head>

<body>
<form id="myForm">

<input type="text" id="fDate"><input type="button" onclick="isValidDate('fDate')" value="Submit">

</form>
</body>
</html>


-------------------------------------------------------------------------

The problem here is that, lets say the user enters 1-Jan-1990, it says the date is invalid. If user enters 01-Jan-1990, it says the date is valid. How do i make 1-Jan-1990 as valid? Both the dates suppose to be valid, but just because 1-Jan-1990 has only 1 digit for the day, it becomes invalid. How do i solve this problem

a1jit
05-03-2004, 08:23 PM
gUYS,

THANKX A lotttttt for all the help..manage to solve the problem. i replaced the if statement with this one

if (arrDate.length==3 && str.length==11 || str.length==10)

THANKX again ...

glenngv
05-04-2004, 04:20 AM
The code I posted is generic. You can validate multiple date fields. With what you've done on it, you can only validate specific fields.

This is how I'd implement it:


<html>
<head>
<script type="text/javascript">
var arr = [];
arr['JAN'] = 0;
arr['FEB'] = 1;
arr['MAR'] = 2;
arr['APR'] = 3;
arr['MAY'] = 4;
arr['JUN'] = 5;
arr['JUL'] = 6;
arr['AUG'] = 7;
arr['SEP'] = 8;
arr['OCT'] = 9;
arr['NOV'] = 10;
arr['DEC'] = 11;

//validates date formats: dd-mmm-yyyy, d-mmm-yyyy
function isValidDate(strDate){
var arrDate = strDate.split('-');
if (arrDate.length==3 && (strDate.length==11 || strDate.length==10)){
var d = new Date(arrDate[2], arr[arrDate[1].toUpperCase()], arrDate[0]);
if (!isNaN(d) && d.getFullYear()==arrDate[2] && d.getMonth()==arr[arrDate[1].toUpperCase()] && d.getDate()==arrDate[0]){
return true;
}
}
return false;
}

function validateFields(f){
if (!isValidDate(f.fDate.value)) {
alert("Invalid date.");
f.fDate.focus();
return false;
}
return true;
}
</script>

</head>
<body>
<form id="myForm" name="myForm" action="javascript:alert('Submitted.')" onsubmit="return validateFields(this)">
<input type="text" id="fDate" name="fDate" />
<input type="submit" value="Submit" />
</form>
</body>
</html>

I modified it in such a way that the month part is case-insensitive.

a1jit
05-04-2004, 05:22 AM
Thank you very very very much bro..

i really really appreciate it!!

thankxxxxx man!!!!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum