cygnusx1z
04-25-2003, 10:43 AM
Hello everyone! I hope someone can help this novice out. I am attempting to combine two scripts that I obtained from other sources (I did not write them myself, I'm not proficient enough yet:D ).
I have built a form with 4 fields. 2 <select> boxes and 2 <text> boxes. I want all 4 fields to be "required" fields. One of the text boxes is a date field. I have copied a date validation JavaScript that works. I have also copied a field validation JavaScript that works.
However, I am doing something wrong when I put both scripts into my HTML file. When I test the form by not filling any fields I get the proper error messages. I then tested by ommitting only the date and that works fine as well. I then test the form by ommitting all but the date. I get the proper error message box but after I click "OK" the form gets "sent" instead of going back to the form.
Also, the date script does not appear to work at all in Netscape 4.75.
One other note, I used FrontPage(it's what my company uses). I have had FrontPage "send" the information collected by the form to a text file.
Any suggestions?
Here's my code:
<HTML>
<HEAD>
<TITLE>Electronic Tally Form</TITLE>
<SCRIPT LANGUAGE = "JavaScript">
<!--
//Validate that fields are completed
function validateFields() {
errmsg = "";
if (document.electTallyForm.name.selectedIndex == 0) {
errmsg += "Please select a NAME\n";
}
if (document.electTallyForm.work_func.selectedIndex == 0) {
errmsg += "Please select a WORK FUNCTION\n";
}
if (document.electTallyForm.ordrnum.value == "") {
errmsg += "Please enter an ORDER NUMBER\n";
}
if (errmsg == "") {
return true
} else {
alert (errmsg);
}
}
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
function isInteger(s){
var i;
for (i = 0; i < s.length; i++){
// Check that current character is number.
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
// All characters are numbers.
return true;
}
function stripCharsInBag(s, bag){
var i;
var returnString = "";
// Search through string's characters one by one.
// If character is not in bag, append to returnString.
for (i = 0; i < s.length; i++){
var c = s.charAt(i);
if (bag.indexOf(c) == -1) returnString += c;
}
return returnString;
}
function daysInFebruary (year){
// February has 29 days in any year evenly divisible by four,
// EXCEPT for centurial years which are not also divisible by 400.
return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
for (var i = 1; i <= n; i++) {
this[i] = 31
if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
if (i==2) {this[i] = 29}
}
return this
}
function isDate(dtStr){
var daysInMonth = DaysArray(12)
var pos1=dtStr.indexOf(dtCh)
var pos2=dtStr.indexOf(dtCh,pos1+1)
var strMonth=dtStr.substring(0,pos1)
var strDay=dtStr.substring(pos1+1,pos2)
var strYear=dtStr.substring(pos2+1)
strYr=strYear
if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
for (var i = 1; i <= 3; i++) {
if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
}
month=parseInt(strMonth)
day=parseInt(strDay)
year=parseInt(strYr)
if (pos1==-1 || pos2==-1){
alert("The date format should be : mm/dd/yyyy")
return false
}
if (strMonth.length<1 || month<1 || month>12){
alert("Please enter a valid month")
return false
}
if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
alert("Please enter a valid day")
return false
}
if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
return false
}
if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
alert("Please enter a valid date")
return false
}
return true
}
function ValidateForm(){
var dt=document.electTallyForm.date
if (isDate(dt.value)==false){
dt.focus()
return false
}
return true
}
-->
</script>
</HEAD>
<BODY BGCOLOR="WHITE" TEXT="BLUE">
<BASEFONT FACE="Arial, Helvetica" SIZE=+2>
<FORM NAME="electTallyForm" METHOD="POST" action="--WEBBOT-SELF--">
<!--webbot bot="SaveResults" u-file="C:\WINNT\Profiles\user1\Desktop\electtallyform_results.txt" s-format="TEXT/CSV" s-label-fields="TRUE" b-reverse-chronology="FALSE" s-builtin-fields --><div align="center">
<center>
<TABLE BORDER="1" CELLPADDING="3" cellspacing="1">
<TR ALIGN="right">
<th bgcolor="#FFCC66">NAME:
<SELECT NAME="name">
<OPTION VALUE="Select One">Select One</OPTION>
<OPTION VALUE="000000">Adams, Pam</OPTION>
<OPTION VALUE="111111">Alender, Tina</OPTION>
<OPTION VALUE="222222">Allen, Bob</OPTION>
</SELECT>
</th>
<th bgcolor="#FFCC66">WORK FUNCTION:
<SELECT NAME="work_func">
<OPTION>Select One</OPTION>
<OPTION>Work Function 1</OPTION>
<OPTION>Work Function 2</OPTION>
<OPTION>Work Function 3</OPTION>
</select>
</th>
</TR>
<TR ALIGN="right">
<th bgcolor="#FFCC66">
<p align="left">DATE: <INPUT NAME="date" size="12" maxlength="10"> <TT> MM/DD/YYYY</TT></th>
<th bgcolor="#FFCC66">
<p align="left">ORDER NUMBER: <INPUT TYPE="text" NAME="ordrnum" SIZE="20"> </th>
<TR>
<TD COLSPAN="2" ALIGN="center" bgcolor="#FFCC66">
<INPUT TYPE="SUBMIT" onclick="validateFields(),ValidateForm()" VALUE="SEND">
<INPUT TYPE="RESET" VALUE="CLEAR">
</TD>
</TR>
</TABLE>
</center>
</div>
</FORM>
</BODY>
</HTML>
I have built a form with 4 fields. 2 <select> boxes and 2 <text> boxes. I want all 4 fields to be "required" fields. One of the text boxes is a date field. I have copied a date validation JavaScript that works. I have also copied a field validation JavaScript that works.
However, I am doing something wrong when I put both scripts into my HTML file. When I test the form by not filling any fields I get the proper error messages. I then tested by ommitting only the date and that works fine as well. I then test the form by ommitting all but the date. I get the proper error message box but after I click "OK" the form gets "sent" instead of going back to the form.
Also, the date script does not appear to work at all in Netscape 4.75.
One other note, I used FrontPage(it's what my company uses). I have had FrontPage "send" the information collected by the form to a text file.
Any suggestions?
Here's my code:
<HTML>
<HEAD>
<TITLE>Electronic Tally Form</TITLE>
<SCRIPT LANGUAGE = "JavaScript">
<!--
//Validate that fields are completed
function validateFields() {
errmsg = "";
if (document.electTallyForm.name.selectedIndex == 0) {
errmsg += "Please select a NAME\n";
}
if (document.electTallyForm.work_func.selectedIndex == 0) {
errmsg += "Please select a WORK FUNCTION\n";
}
if (document.electTallyForm.ordrnum.value == "") {
errmsg += "Please enter an ORDER NUMBER\n";
}
if (errmsg == "") {
return true
} else {
alert (errmsg);
}
}
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
function isInteger(s){
var i;
for (i = 0; i < s.length; i++){
// Check that current character is number.
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
// All characters are numbers.
return true;
}
function stripCharsInBag(s, bag){
var i;
var returnString = "";
// Search through string's characters one by one.
// If character is not in bag, append to returnString.
for (i = 0; i < s.length; i++){
var c = s.charAt(i);
if (bag.indexOf(c) == -1) returnString += c;
}
return returnString;
}
function daysInFebruary (year){
// February has 29 days in any year evenly divisible by four,
// EXCEPT for centurial years which are not also divisible by 400.
return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
for (var i = 1; i <= n; i++) {
this[i] = 31
if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
if (i==2) {this[i] = 29}
}
return this
}
function isDate(dtStr){
var daysInMonth = DaysArray(12)
var pos1=dtStr.indexOf(dtCh)
var pos2=dtStr.indexOf(dtCh,pos1+1)
var strMonth=dtStr.substring(0,pos1)
var strDay=dtStr.substring(pos1+1,pos2)
var strYear=dtStr.substring(pos2+1)
strYr=strYear
if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
for (var i = 1; i <= 3; i++) {
if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
}
month=parseInt(strMonth)
day=parseInt(strDay)
year=parseInt(strYr)
if (pos1==-1 || pos2==-1){
alert("The date format should be : mm/dd/yyyy")
return false
}
if (strMonth.length<1 || month<1 || month>12){
alert("Please enter a valid month")
return false
}
if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
alert("Please enter a valid day")
return false
}
if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
return false
}
if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
alert("Please enter a valid date")
return false
}
return true
}
function ValidateForm(){
var dt=document.electTallyForm.date
if (isDate(dt.value)==false){
dt.focus()
return false
}
return true
}
-->
</script>
</HEAD>
<BODY BGCOLOR="WHITE" TEXT="BLUE">
<BASEFONT FACE="Arial, Helvetica" SIZE=+2>
<FORM NAME="electTallyForm" METHOD="POST" action="--WEBBOT-SELF--">
<!--webbot bot="SaveResults" u-file="C:\WINNT\Profiles\user1\Desktop\electtallyform_results.txt" s-format="TEXT/CSV" s-label-fields="TRUE" b-reverse-chronology="FALSE" s-builtin-fields --><div align="center">
<center>
<TABLE BORDER="1" CELLPADDING="3" cellspacing="1">
<TR ALIGN="right">
<th bgcolor="#FFCC66">NAME:
<SELECT NAME="name">
<OPTION VALUE="Select One">Select One</OPTION>
<OPTION VALUE="000000">Adams, Pam</OPTION>
<OPTION VALUE="111111">Alender, Tina</OPTION>
<OPTION VALUE="222222">Allen, Bob</OPTION>
</SELECT>
</th>
<th bgcolor="#FFCC66">WORK FUNCTION:
<SELECT NAME="work_func">
<OPTION>Select One</OPTION>
<OPTION>Work Function 1</OPTION>
<OPTION>Work Function 2</OPTION>
<OPTION>Work Function 3</OPTION>
</select>
</th>
</TR>
<TR ALIGN="right">
<th bgcolor="#FFCC66">
<p align="left">DATE: <INPUT NAME="date" size="12" maxlength="10"> <TT> MM/DD/YYYY</TT></th>
<th bgcolor="#FFCC66">
<p align="left">ORDER NUMBER: <INPUT TYPE="text" NAME="ordrnum" SIZE="20"> </th>
<TR>
<TD COLSPAN="2" ALIGN="center" bgcolor="#FFCC66">
<INPUT TYPE="SUBMIT" onclick="validateFields(),ValidateForm()" VALUE="SEND">
<INPUT TYPE="RESET" VALUE="CLEAR">
</TD>
</TR>
</TABLE>
</center>
</div>
</FORM>
</BODY>
</HTML>