Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-27-2003, 10:28 AM   #1
Registered: Mar 2003
Location: Macondo
Distribution: Mandrake 9.1, 10.1, SuSE 8.1 pro, 10.1, Red Hat 8.0/9.0
Posts: 380

Rep: Reputation: 30
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?

Here is the code:


print "<html>";
print "<head>";
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">";
print "<link rel=\"stylesheet\" href=\"/bugzilla/css/overzetten.css\">";

############## JavaScript Date Validation BEGIN ##############

print <<DateValJS;

<script type="text/javascript" language="JavaScript">

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();; //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();; //optional
return false;



############## JavaScript Date Validation END ##############

print "</head>";

print "<body>";
print "<form method=\"post\" action=\"enter_overzetten.cgi\" enctype=\"multipart/formdata\" name=\"enter_overzetten\" >";
print "<table border=\"0\">";

print "<tr>";
print "<td valign=\"middle\" align=\"left\"><font face=\"Arial, Helvetica, sans-serif\" size=\"2\" color=\"#000000\">ontw_test_overzetdatum</font></th>";
print "<td valign=\"middle\" align=\"left\"><input type=text name=\"ontw_test_overzetdatum\" size=40 onclick=\"highlight(event)\"
onblur=\"return checkdate(this.form.ontw_test_overzetdatum)\"></th>"; <---THIS IS THE FIELD!!
print "</tr>";

Last edited by vous; 08-27-2003 at 10:52 AM.
Old 08-27-2003, 01:17 PM   #2
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 66
Check the source of the page once it is created. Some od hte text has probably been assumed to be variables. ie the $ signs.
Old 08-27-2003, 05:34 PM   #3
Senior Member
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
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.

Just something to ponder I guess.
Old 08-27-2003, 05:49 PM   #4
Senior Member
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
I guess I should spend a bit more time dabbling - still got to do the range checking like you're doing - oh well


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to print my CGI programs output inside an HTML zaveko Programming 9 10-11-2005 06:37 PM
Javascript Code doesnt work w/ firefox? swatward Programming 1 04-18-2005 07:54 PM
CGI to work out side of cgi-bin? crashedspine Linux - Newbie 13 09-02-2004 09:49 PM
How do I get javascript to work with Opera? Aerlock Linux - Software 0 11-17-2003 10:28 PM
JavaScript applets not work in Linux? Conman Linux - Newbie 2 07-27-2002 01:41 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:30 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration