473,657 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="JavaS cript" type="text/javascript">
var fieldstocheck = new Array();
fieldnames = new Array();

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

eval("document. subscribeform.e lements['"+fieldstochec k[i]+"'].focus()");
return false;
}
}

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

function compareEmail()
{
return (document.subsc ribeform.elemen ts["email"].value ==
document.subscr ibeform.element s["emailconfi rm"].value);
}
</script> <form
action="http://www.whatever.co m/newsletter/lists/?p=subscribe"
name="subscribe form" method="post" target="_self">
<div align="left"><f ont size="2" face="Verdana">
<input type="checkbox" name="list[1]" value=signup checked />
Nieuwsbrief 'Impuls' </font></div>
<div align="left" class="listdesc ription"> <font size="2"
face="Verdana">
<input type=hidden name="listname[1]" value="Nieuwsbr ief
Impuls"/>
</font></div>
<div align="left"><f ont size="2" face="Verdana">
<input name="list[2]" type="checkbox" value=signup checked
/>
Aanbiedingen voor niet-leden</font></div>
<div align="left" class="listdesc ription">
<input type=hidden name="listname[2]" value="Aanbiedi ngen
voor niet-leden"/>
<br>
</div>
<table border=0>
<tr>
<td width="96"><div class="required ">Email:</div></td>
<td width="260" class="attribut einput"><input type=text
name=email value="" size="40">
<script language="JavaS cript" type="text/javascript">
addFieldToCheck ("email","Email ");
</script></td>
</tr>
<tr>
<td>Bevestig email:</td>
<td class="attribut einput"><input type=text
name=emailconfi rm size="40">
<script language="JavaS cript" type="text/javascript">
addFieldToCheck ("emailconfirm" ,"Bevestig email");
</script></td>
</tr>
<tr>
<td><font size="2" face="Verdana"> Lid:</font></td>
<td class="attribut einput"><font size="2" face="Verdana">
<select name="attribute 6" id="select2">
<option value="3">Ja</option>
<option value="4">Nee</option>
</select>
<!--0-->
<script language="JavaS cript" 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="attribut einput"><font size="2" face="Verdana">
<input name="attribute 4" type="text" id="PassNr2"
size="15" maxlength="15">
<script language="JavaS cript" type="text/javascript">
addFieldToCheck ("attribute4"," Pas nr");
</script>
</font> </td>
</tr>
<input type=hidden name="htmlemail " value="1">
</table>
<div align="left"><f ont size="2" face="Verdana"> <br>
<input name="subscribe " type="submit" id="subscribe"
value="Aanmelde n" 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 5919
pl***********@g mail.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="JavaS cript" 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<fieldsto check.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.attribute 6.focus) form.attribute6 .focus();
return false;
}
if
(eval("document .subscribeform. elements['"+fieldstochec k[i]+"'].value")
== "") {
eval is almost never required and is not needed here at all. The
equivalent would be:

if (document.subsc ribeform.elemen ts[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="retur n checkform(this) ;"
If checkform returns false, the form will not submit.

action="http://www.whatever.co m/newsletter/lists/?p=subscribe"
name="subscribe form" method="post" target="_self">
<div align="left"><f ont 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="listdesc ription"> <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
4975
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" you can be pretty tough on querystrings, if you use them at all. But user input could have a name like Mc'Donald, and we would not like quotes (wether single or double) in input to a database or an asp script. Though I beleive more dangerous in...
13
9605
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 assigned to a variable and output using document.write. (Note, there is no submit button or other form elements. Basically
1
3644
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 (as is the case currently). 1. On selecting a topic, the Skills menu will only show those Topics with dependencies (which will be provided) and vice versa.
3
2793
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
1951
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 Test(){
26
3211
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" are passed onto this page from a previous page and I haven't been able to figure out how to show the passed values in the list boxes when the page loads. All variables passed perfectly before I added the dependent list code below. Currently, when...
2
1751
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 been migrating some relatively straight-forward scripts-with-GUIs from Linux to OSX, and, while I used FLTK on Linux, I could not get pyfltk to install on OSX. PyGUI was so straightforward, though. Made me a very happy bunny. Fortunately, I wrote...
1
2956
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. Making a selection from list/box A (Qtr) selects a fiscal quarter, which then refreshes the values in list/box B (Mth), which shows the 3 months in that fiscal quarter, which then refreshes the values in list/box C (MthDate), which returns the date...
0
8385
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8303
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8723
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8502
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8602
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7316
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.