Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form and JavaScript

Status
Not open for further replies.

samesale

Programmer
Sep 15, 2003
133
0
0
US
I found this code on the Internet. I modified it by adding a new line to the form. However, it does not perform check on the birth date. I know that the W variable is undefined but it must be picking a value for W somewhere in the program. Since, I am new to JavaScript, I need help as how to make the last field check itself.

<HEAD>

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
FormName = "form" //Name of the form
CheckName = "check" //Name of the checkboxes (without 1,2,3,...10,11,12...)
TextName = "text" //Name of the textfields (without 1,2,3,...10,11,12...)

var Condition=new Array(
'a!=""', //Must be filled
'a!=""', //Must be filled
'a!=""&&a.length>5&&a.indexOf("@")!=-1&&a.indexOf(".")!=-1', //Must be longer than 5 characters, have @, and atleast one '.'
'a.length>7&&!(a.split("/")[0]*1>12)&&!(a.split("/")[1]*1>31)&&!(a.split("/")[2]*1<1900)' //Must be longer than 7 characters, first (two) digit(s) NOT greater than 12, second (two) digit(s) NOT greater than 31 and last 4 greater than 1900
);
function docheck(w) {
eval("a=document." + FormName + "." + TextName + w + ".value");
if (eval(Condition[w])) eval("document." + FormName + "." + CheckName + w + ".checked=true");
else eval("document." + FormName + "." + CheckName + w + ".checked=false");
}
// End -->
</script>

</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document -->

<BODY>

<form name=form>
<input type=checkbox name=check0 onClick="return false"> First: <input type=text name=text0 onBlur='docheck("0")'><BR>
<input type=checkbox name=check1 onClick="return false"> Last: <input type=text name=text1 onBlur='docheck("1")'><BR>
<input type=checkbox name=check2 onClick="return false"> E-Mail: <input type=text name=text2 onBlur='docheck("2")'><BR>
<input type=checkbox name=check3 onClick="return false"> Telephone: <input type=text name=text3 onBlur='docheck("3")'><BR
<input type=checkbox name=check4 onClick="return false"> Birthday (mm/dd/yyyy): <input type=text name=text4 onBlur='docheck("4")'>
<BR>
</form>
<!-- Script Size: 2.25 KB -->
 
Well, first of all your checkbox for the Birthday field isn't showing up because you didn't end the BR tag:
Code:
<input type=checkbox name=check3 onClick="return false"> Telephone: <input type=text name=text3 onBlur='docheck("3")'><BR
Change that to
Code:
<input type=checkbox name=check3 onClick="return false"> Telephone: <input type=text name=text3 onBlur='docheck("3")'><BR[red]>[/red]
Second, the birthday field is running a check on check4. It uses the 4th entry of the array for error-checking. One problem - there is no 4th entry. But do not change it to docheck("3"). This would check the value of check3, which is the telephone field. Instead, to modify this very unflexible code, change your script to this instead:
Code:
<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
FormName = "form" //Name of the form
CheckName = "check" //Name of the checkboxes (without 1,2,3,...10,11,12...)
TextName = "text" //Name of the textfields (without 1,2,3,...10,11,12...)

var Condition=new Array(
'a!=""', //Must be filled
'a!=""', //Must be filled
'a!=""&&a.length>5&&a.indexOf("@")!=-1&&a.indexOf(".")!=-1', //Must be longer than 5 characters, have @, and atleast one '.'
'a.length>7&&!(a.split("/")[0]*1>12)&&!(a.split("/")[1]*1>31)&&!(a.split("/")[2]*1<1900)'[red],[/red] //Must be longer than 7 characters, first (two) digit(s) NOT greater than 12, second (two) digit(s) NOT greater than 31 and last 4 greater than 1900
[red]'a.length>7&&!(a.split("/")[0]*1>12)&&!(a.split("/")[1]*1>31)&&!(a.split("/")[2]*1<1900)' //Must be longer than 7 characters, first (two) digit(s) NOT greater than 12, second (two) digit(s) NOT greater than 31 and last 4 greater than 1900[/red]
);
function docheck(w) {
eval("a=document." + FormName + "." + TextName + w + ".value");
if (eval(Condition[w])) eval("document." + FormName + "." + CheckName + w + ".checked=true");
else eval("document." + FormName + "." + CheckName + w + ".checked=false");
}
// End -->
</script>
The code in red indicates the changes being made. Basically, you're adding a comma after the 3rd array entry (indicating that there is another entry to add) and adding the same entry as the 3rd but for the 4th entry. I think that might be confusing, but so is the logic behind coding this way. Anyway it works for me ;)
 
Thanks for the suggestion. I did add > and it worked. However, when add the red code, it does not work. But when I removed the red code, it still does not work. Here is the new code:

<HEAD>

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
FormName = "form" //Name of the form
CheckName = "check" //Name of the checkboxes (without 1,2,3,...10,11,12...)
TextName = "text" //Name of the textfields (without 1,2,3,...10,11,12...)

var Condition=new Array(
'a!=""', //Must be filled
'a!=""', //Must be filled
'a!=""&&a.length>5&&a.indexOf("@")!=-1&&a.indexOf(".")!=-1', //Must be longer than 5 characters, have @, and atleast one '.'
'a.length>7&&!(a.split("/")[0]*1>12)&&!(a.split("/")[1]*1>31)&&!(a.split("/")[2]*1<1900)' //Must be longer than 7 characters, first (two) digit(s) NOT greater than 12, second (two) digit(s) NOT greater than 31 and last 4 greater than 1900
'a.length>7&&!(a.split("/")[0]*1>12)&&!(a.split("/")[1]*1>31)&&!(a.split("/")[2]*1<1900)' //Must be longer than 7 characters, first (two) digit(s) NOT greater than 12, second (two) digit(s) NOT greater than 31 and last 4 greater than 1900
);
function docheck(w) {
eval("a=document." + FormName + "." + TextName + w + ".value");
if (eval(Condition[w])) eval("document." + FormName + "." + CheckName + w + ".checked=true");
else eval("document." + FormName + "." + CheckName + w + ".checked=false");
}
// End -->
</script>

</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document -->

<BODY>

<form name=form>
<input type=checkbox name=check0 onClick="return false"> First: <input type=text name=text0 onBlur='docheck("0")'><BR>
<input type=checkbox name=check1 onClick="return false"> Last: <input type=text name=text1 onBlur='docheck("1")'><BR>
<input type=checkbox name=check2 onClick="return false"> E-Mail: <input type=text name=text2 onBlur='docheck("2")'><BR>
<input type=checkbox name=check3 onClick="return false"> Telephone: <input type=text name=text3 onBlur='docheck("3")'><BR>
<input type=checkbox name=check4 onClick="return false"> Birthday (mm/dd/yyyy): <input type=text name=text4 onBlur='docheck("4")'>
<BR>
</form>
<!-- Script Size: 2.25 KB -->


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top