Name and Date Validation?
I don't know much about scripting yet but I'm trying to find 2 small validation programs, or figure out how to write them myself.
And for another, I need to validate whether or not a user typed in the date in the correct format.
(meaning 12/8/2009 or 12-8-2009. it can have either of those 2 symbols.)
And again, have a pop up window stating whether or not you did.
Here is the tricky bit, figuring if the date string is a real date:
var A= s.split(/\D+/), er= 'bad date',
y= +A, m= parseInt(A, 10), d= parseInt(A, 10);
if(d> 31 || m> 12 || d<0 || m<0) throw er;
case 9: case 4: case 6: case 11:
if(d> 30) throw er;
if(y%4 || (!y%100 && y%400)) throw er;
else if(d> 28) throw er;
s= s.replace(/(\d)\-/g,'$1 ');
if(!d) throw er;
return new Date(d)
var s= "2-29-2008 12:05:00 PM";// good date
s= "2/29/2008"; // good date
var s= "12-32-2009 12:05:00 PM";// bad date
s= "12/32/2009";//bad date
Last edited by mrhoo; 12-08-2009 at 11:34 PM.
How could you know whether the user, when entering 12/8/2009, had in mind the 12th of August or the 8th of December? Both are valid dates, aren't they? Never relay on the user's good will (nor suppose the user is always an American, or always an European) and don't let him the possibility to make mistakes. Use 3 separate inputs (or select elements), one for the Date, one for the Month and one for the Year.
Originally Posted by mvbf987
Oh wow I didn't expect anyone to just give code for it. Thank you I'll give it a shot.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread