473,386 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Validating TextField dependent on List/Menu value

Hallo NG,

I am new to JavaScript and would really appreciate any help to solve my
problem.
I am using the blow code in my form to validate form fields. What I
would like to accomplish is that if when the list/menu (attribute6)
value is "Ja" then to make the TextField Pas Nr (attribute4)
required. And if when the list/menu (attribute6) value is "Nee" to
make the TextField Pas Nr (attribute4) not required.

I have tried to adjust the code with no success. I give up and hope
that someone can help me to the right direction!

Thank you in advance,
-Platostoteles

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="41%" border="0" align="center" id="0">
<tr>
<td height="291" colspan="2" align="left" valign="top">
<!-- newsletter -->
<script language="JavaScript" type="text/javascript">
var fieldstocheck = new Array();
fieldnames = new Array();

function checkform() {
for (i=0;i<fieldstocheck.length;i++) {
if
(eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].value")
== "") {
alert("Vul a.u.b. het volgende verplichte veld in:
"+fieldnames[i]);

eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].focus()");
return false;
}
}

if(! compareEmail())
{
alert("De ingevulde Email adressen komen niet overeen");
return false;
}
return true;
}
function addFieldToCheck(value,name) {
fieldstocheck[fieldstocheck.length] = value;
fieldnames[fieldnames.length] = name;
}

function compareEmail()
{
return (document.subscribeform.elements["email"].value ==
document.subscribeform.elements["emailconfirm"].value);
}
</script> <form
action="http://www.whatever.com/newsletter/lists/?p=subscribe"
name="subscribeform" method="post" target="_self">
<div align="left"><font size="2" face="Verdana">
<input type="checkbox" name="list[1]" value=signup checked />
Nieuwsbrief 'Impuls' </font></div>
<div align="left" class="listdescription"> <font size="2"
face="Verdana">
<input type=hidden name="listname[1]" value="Nieuwsbrief
Impuls"/>
</font></div>
<div align="left"><font size="2" face="Verdana">
<input name="list[2]" type="checkbox" value=signup checked
/>
Aanbiedingen voor niet-leden</font></div>
<div align="left" class="listdescription">
<input type=hidden name="listname[2]" value="Aanbiedingen
voor niet-leden"/>
<br>
</div>
<table border=0>
<tr>
<td width="96"><div class="required">Email:</div></td>
<td width="260" class="attributeinput"><input type=text
name=email value="" size="40">
<script language="JavaScript" type="text/javascript">
addFieldToCheck("email","Email");
</script></td>
</tr>
<tr>
<td>Bevestig email:</td>
<td class="attributeinput"><input type=text
name=emailconfirm size="40">
<script language="JavaScript" type="text/javascript">
addFieldToCheck("emailconfirm","Bevestig email");
</script></td>
</tr>
<tr>
<td><font size="2" face="Verdana">Lid:</font></td>
<td class="attributeinput"><font size="2" face="Verdana">
<select name="attribute6" id="select2">
<option value="3">Ja</option>
<option value="4">Nee</option>
</select>
<!--0-->
<script language="JavaScript" type="text/javascript">
addFieldToCheck("attribute6","Lid");
</script>
</font></td>
</tr>
<tr>
<td><div class="required"><font size="2" face="Verdana">Pas
nr.</font></div></td>
<td class="attributeinput"><font size="2" face="Verdana">
<input name="attribute4" type="text" id="PassNr2"
size="15" maxlength="15">
<script language="JavaScript" type="text/javascript">
addFieldToCheck("attribute4","Pas nr");
</script>
</font> </td>
</tr>
<input type=hidden name="htmlemail" value="1">
</table>
<div align="left"><font size="2" face="Verdana"><br>
<input name="subscribe" type="submit" id="subscribe"
value="Aanmelden" onClick="return checkform();">
<br>
<br>
<br>
</font> </div>
</form>
<font size="2" face="Verdana">&nbsp; </font></td>
</tr>
</table>
</body>
</html>

Jan 19 '06 #1
1 5892
pl***********@gmail.com wrote:
Hallo NG,

I am new to JavaScript and would really appreciate any help to solve my
problem.
I am using the blow code in my form to validate form fields. What I
would like to accomplish is that if when the list/menu (attribute6)
value is "Ja" then to make the TextField Pas Nr (attribute4)
required. And if when the list/menu (attribute6) value is "Nee" to
make the TextField Pas Nr (attribute4) not required.

I have tried to adjust the code with no success. I give up and hope
that someone can help me to the right direction!

Thank you in advance,
-Platostoteles

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
It might be time to change to strict, given that it has been around for
6 years now.
<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="41%" border="0" align="center" id="0">
When posting code, keep examples to a minimum that show the problem.
Otherwise you are posting lots of stuff that just isn't necessary.

<tr>
<td height="291" colspan="2" align="left" valign="top">
<!-- newsletter -->
<script language="JavaScript" type="text/javascript">
The language attribute is deprecated, keep type.

var fieldstocheck = new Array();
fieldnames = new Array();
I'll guess that fieldstocheck will contain the names of form elements to
check? But you haven't overcome the issue of how to put the test into
the array - there is no simple answer to that.


function checkform() {
Instead of using a submit button onclick attribute to call checkform(),
use the form onsubmit attribute. If you pass a reference to the form
from the onclick (see below), you don't have to worry about getting a
reference to the form from the function:

function checkform(form) {

for (i=0;i<fieldstocheck.length;i++) {
Variables should be kept local wherever possible, especially counters
like 'i'.

But anyhow, all you want to do is:

var t = form.attribute4[form.attribute4.selectedIndex].value;
if ('Ja' == t && "" != form.attribute6.value){
alert("Vul a.u.b. het volgende verplichte veld in Pas nr.";
if (form.attribute6.focus) form.attribute6.focus();
return false;
}
if
(eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].value")
== "") {
eval is almost never required and is not needed here at all. The
equivalent would be:

if (document.subscribeform.elements[fieldstocheck[i]].value == "") {

but I've changed that line anyway.
[...]
</script> <form
Add an onsubmit handler and pass a reference to the form using 'this':

onsubmit="return checkform(this);"
If checkform returns false, the form will not submit.

action="http://www.whatever.com/newsletter/lists/?p=subscribe"
name="subscribeform" method="post" target="_self">
<div align="left"><font size="2" face="Verdana">
<input type="checkbox" name="list[1]" value=signup checked />
This is HTML, so use HTML markup not faux XHTML - ditch the '/':

<input type="checkbox" name="list[1]" value="signup" checked>
You are also better off to always enclose attribute values in quotes,
even when not required.

Nieuwsbrief 'Impuls' </font></div>
<div align="left" class="listdescription"> <font size="2"
face="Verdana">


The font element is deprecated in HTML 4, use CSS instead. There is no
need to put all those divs in there, just use one div, style it
appropriately and use BR to break lines. Forms must contain a block
element, so wrap all the elements inside the form in a single div.

The above is untested, but should give you an idea of what to do.

[...]

--
Rob
Jan 20 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mats | last post by:
It's good practice to validate input, not only where it should be coming from, but from anywhere it's possible to change or add input for a "client". If all user input is transfered using "post"...
13
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be...
1
by: paakwesi | last post by:
I'm looking to modify the javascript behavior on http://research.yale.edu/%7Ekamusi/exercises/learners/index.php to do two things: Initial State: Both menus are populated with all their entries...
3
by: TheSteph | last post by:
Hi Experts ! I have a Winform Program in C# / .NET 2.0 I would like to ensure that a value in a TextBox is a valid Int32 when user get out of it (TextBox loose focus)
16
by: shyamg | last post by:
Hi, this is my javascript validating the fields in mozilla FF but its working and validating only one field. how to write the and how to works the script .................. function...
26
by: jmartmem | last post by:
Greetings, I have an ASP page with two dynamic dependent list boxes written in JavaScript. My dependent lists work great, but my problem is that the values for "Program_Name" and "Project_Name"...
2
by: Hugh | last post by:
The PyGUI website specified this place as the place for general discussion about it, so here goes.... First off - thanks for something that is so straightforward to get running on OSX... I've...
1
by: jmartmem | last post by:
Greetings, I have a nagging problem with client-side dynamic dependent list boxes that perhaps someone can help me troubleshoot. I have a form with a series of dynamic dependent list boxes....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.