Bhavya
12-03-2004, 06:32 AM
I have a JavaScript form as follows.
"Item Ref No." is mandatory only if the user chooses "type2" in "Item type" field. This part works fine however "Item Ref No." needs to contain same number of entries as "No. of Items". At the moment, if "No of Items" is more than one, and the user enters only one value for "Item Ref No", still the user can submit the form.
Any help would be grately appreciated!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">
<title>Example</title>
<script type="text/javascript">
<!--
function require (e) {
if (/\S/.test (e.value)) return true;
alert (['Please enter a value for field', e.previousSibling.data].join (' '));
e.focus();
return false;
}
function check (f) {
switch (f.icategory.selectedIndex) {
case 1 :
if (!require (f.date)) return false;
break;
}
switch (f.itype.selectedIndex) {
case 1 :
if (!require (f.refno)) return false;
break;
}
}
// -->
</script>
</head>
<body>
<form action="" onsubmit="return check (this)">
<h1>Item List</h1>
<label>Item category: <select name="icategory">
<option value="category1" selected>Category1</option>
<option value="category2">Category2</option>
</select></label>
<p>
<label>Item Type: <select name="itype">
<option value="type1" selected>Type1</option>
<option value="type2">Type2</option>
</select></label>
</p>
<p>
<label>No. of Items: <input name="number" type="text"></label>
</p>
<p>
<label>Item Reference Number:<input name="refno" type="text"></label>
</p>
<p>
<label>Delivery Date: <input name="date" type="text"></label></p>
</p>
<tr>
<td ALIGN=LEFT width="222"> </td>
<td width="218"> <input type=submit value=Submit> <INPUT type=reset value="Clear Form"></td>
</tr>
</table>
</form>
</body>
</html>
Thanks
"Item Ref No." is mandatory only if the user chooses "type2" in "Item type" field. This part works fine however "Item Ref No." needs to contain same number of entries as "No. of Items". At the moment, if "No of Items" is more than one, and the user enters only one value for "Item Ref No", still the user can submit the form.
Any help would be grately appreciated!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">
<title>Example</title>
<script type="text/javascript">
<!--
function require (e) {
if (/\S/.test (e.value)) return true;
alert (['Please enter a value for field', e.previousSibling.data].join (' '));
e.focus();
return false;
}
function check (f) {
switch (f.icategory.selectedIndex) {
case 1 :
if (!require (f.date)) return false;
break;
}
switch (f.itype.selectedIndex) {
case 1 :
if (!require (f.refno)) return false;
break;
}
}
// -->
</script>
</head>
<body>
<form action="" onsubmit="return check (this)">
<h1>Item List</h1>
<label>Item category: <select name="icategory">
<option value="category1" selected>Category1</option>
<option value="category2">Category2</option>
</select></label>
<p>
<label>Item Type: <select name="itype">
<option value="type1" selected>Type1</option>
<option value="type2">Type2</option>
</select></label>
</p>
<p>
<label>No. of Items: <input name="number" type="text"></label>
</p>
<p>
<label>Item Reference Number:<input name="refno" type="text"></label>
</p>
<p>
<label>Delivery Date: <input name="date" type="text"></label></p>
</p>
<tr>
<td ALIGN=LEFT width="222"> </td>
<td width="218"> <input type=submit value=Submit> <INPUT type=reset value="Clear Form"></td>
</tr>
</table>
</form>
</body>
</html>
Thanks