View Full Version : validating dates in the format DD-MON-YY
hendryk
08-12-2002, 11:03 AM
Hi,
I want to be able to validate a date as the user types it into a textfield. The date must be in the format DD-MON-YY
i.e 20-MAY-02. Does anybody know the code to do this?
Katie
x_goose_x
08-12-2002, 01:49 PM
Pretty simple. Just searches for the - . Any good or do you want more?
<script>
function ver_date() {
thedate = document.form1.mydate.value;
if (thedate.charAt(thedate.length-3)!="-"||thedate.charAt(2)!="-") {
alert('Incorrect Date')
}
}
</script>
<form name="form1">
<input type="text" name="mydate" size="20" onChange="ver_date();" value="12-DDDDDDDDDDDD-45">
</form>
nolachrymose
08-12-2002, 02:03 PM
function validDate(dateStr) {
var re=/\d{2}-[a-z]{3}-\d{2}/i;
return (re.test(dateStr));
}
Hope that helps!
Happy coding! :)
mordred
08-12-2002, 02:18 PM
A slight amendment by a nitpicker like me:
function validDate(dateStr) {
var re=/^\d{2}-[a-z]{3}-\d{2}$/i;
return (re.test(dateStr));
}
hendryk
08-12-2002, 02:50 PM
Originally posted by x_goose_x
Pretty simple. Just searches for the - . Any good or do you want more?
<script>
function ver_date() {
thedate = document.form1.mydate.value;
if (thedate.charAt(thedate.length-3)!="-"||thedate.charAt(2)!="-") {
alert('Incorrect Date')
}
}
</script>
<form name="form1">
<input type="text" name="mydate" size="20" onChange="ver_date();" value="12-DDDDDDDDDDDD-45">
</form>
This is good but how do I make sure that the middle three characters are not numbers and are an abbreviation for a month of the year.
Thanks
Katie
x_goose_x
08-12-2002, 03:46 PM
Checks for - , checks to make sure day & year are numbers, checks if month is in array.
<script>
mon = new Array();
mon[0] = "jan";
mon[1] = "feb";
mon[2] = "mar";
mon[3] = "apr";
//... finish this
go = "";
z = new Array();
function ver_date() {
thedate = document.form1.mydate.value.toLowerCase();
month = thedate.substring(3,thedate.length-3);
z[0] = thedate.charCodeAt(0);
z[1] = thedate.charCodeAt(1);
z[2] = thedate.charCodeAt(thedate.length-2);
z[3] = thedate.charCodeAt(thedate.length-1);
for (y=0; y<z.length; y++) {
if (z[y]>57||z[y]<48) {
go = false;
}
}
for (x=0; x<mon.length; x++) {
if (month==mon[x]) {
go = true;
}
}
if (thedate.charAt(thedate.length-3)!="-"||thedate.charAt(2)!="-") {
go = false;
}
if (!go) {
alert("Incorrect Date!")
}
}
</script>
<form name="form1">
<input type="text" name="mydate" size="20" onChange="ver_date();" value="12-hello-45">
</form>
BrightNail
08-13-2002, 12:08 AM
hmmm,
all that code seems very long and overbearing..but I am PROBABLY wrong...I would try something like so...
var verifydate=/^\d{2}\-\D{3}\-\d{2}$/
!verifydate.test(document.form.date.value)
2 numbers a dash, 3 letters, a dash, 2 numbers
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.