JavaScript inside a CGI for date validation....doesn't work!!
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Mandrake 9.1, 10.1, SuSE 8.1 pro, 10.1, Red Hat 8.0/9.0
Posts: 380
Rep:
JavaScript inside a CGI script for date validation....doesn't work!!
Hello All, I'm trying to validate a date field on the client side with JavaScript inside a CGI script....
It is supposed to come up with a pop up window telling the user he has not entered the date correctly. This should happen after the user leaves the field. What is happening is nothing, it does not come up with the pop-up, nor do I get error messages.
The JavaScript standalone works but when I put inside the CGI script it doesn't....would anybody be able to give me a hand?
function checkdate(xxfield) {
if (!xxfield) return;
if (!xxfield.value || /^\s+$/.test(xxfield.value)) { //empty string/spaces
return true;
}
if (!/^\d{2}\/\d{2}\/\d{4}$/.test(xxfield.value)) { // mm/dd/yyyy format
alert('Please enter a date in this format:\n\n mm/dd/yyyy.');
xxfield.focus();
xxfield.select(); //optional
return false;
}
var strSeparator = '/';
var arrayDate = xxfield.value.split(strSeparator);
var arrayLookup = {'01':31,'03':31,'04':30,'05':31,'06':30,'07':31,'08':31,'09':30,'10':31,'11':30,'12':31};
var intDay = parseInt(arrayDate[1]);
if (arrayLookup[arrayDate[0]] != null) {
if (intDay <= arrayLookup[arrayDate[0]] && intDay != 0) //correct day for month
return true;
} else if (arrayDate[0] == '02') { //check Feb. leap/non-leap
var intYear = parseInt(arrayDate[2]);
if (((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
return true;
}
alert('This is not a valid date.\n\nPlease re-enter in the format mm/dd/yyyy.');
xxfield.focus();
xxfield.select(); //optional
return false;
}
</script>
DateValJS
############## JavaScript Date Validation END ##############
I only dabble lightly with html and javascript but an easy way to check if a date is valid is to have three different text inputs for day,month and year , parseInt the three different values and try to create a new Date object
with them, if the new Date object contains "Invalid Date" then you know it's a dodgy date.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.