Form field
validation - This example shows you how to make sure that a field
is filled in.
<script
language="JavaScript" type="text/javascript">
function validate()
{
if (document.title_of_form.form_box_name.value.length == 0) {
alert("You must fill in Submitted By");
return false;
}
ischecked=false;
if (document.title_of_form.name_of_checkbox.checked) {
if (ischecked==false)
{
alert("You must check at least one box");
return false;
}
return true;
}
</script>
Populate
listbox from selection - This example shows how to populate listbox
after making another listbox selection.
<script
language="javascript">
function setOption(form, index) {
for (var i = 0; i < form.Listbox1.length; i++) {
form.Listbox1.options[i].text = "";
form.Listbox1.options[i].value = "";
}
if (index == 1) {
form.Listbox1.options[0].text = "Red";
form.Listbox1.options[0].value = "Red";
form.Listbox1.options[1].text = "Blue";
form.Listbox1.options[1].value = "Blue";
} else if (index == 2) {
form.Listbox1.options[0].text = "Green";
form.Listbox1.options[0].value = "Green";
form.Listbox1.options[1].text = "Black";
form.Listbox1.options[1].value = "Black";
}
for (var i = 0; i < form.Listbox2.length; i++) {
form.Listbox2.options[i].text = "";
form.Listbox2.options[i].value = "";
}
if (index == 1) {
form.Listbox2.options[0].text = "Blue";
form.Listbox2.options[0].value = "Blue";
form.Listbox2.options[1].text = "Red";
form.Listbox2.options[1].value = "Red";
} else if (index == 2) {
form.Listbox2.options[0].text = "Black";
form.Listbox2.options[0].value = "Black";
form.Listbox2.options[1].text = "Green";
form.Listbox2.options[1].value = "Green";
}
form.Listbox1.selectedIndex = 0;
form.Listbox2.selectedIndex = 0;
}
</script>
HTML Code:
<form
action="example.php" name="main" method="post">
<p>Colors: <select name="Type" id="Type"
onChange="setOption(this.form, this.selectedIndex)">
<option value="">Select:</option>
<option value="Colors:Red/Blue">Colors: Red/Blue</option>
<option value="Colors:Green/Black">Colors: Green/Black</option>
</select></p>
<p> Color 1: <select name="Listbox1" size="1"
id="Listbox1">
<option></option>
<option></option>
</select></p>
<p>Color 2: <select name="Listbox2" size="1"
id="Listbox2">
<option></option>
<option></option>
</select>
</form>
To see
in use - click here
Add/Remove
items to listbox - This example shows how to add and remove
items to a listbox.
<script language="javascript" type="text/javascript">
function AddItemtoListbox()
{
var item = "MyFirstItem";
var opt = document.createElement("option");
document.getElementById("Listbox_id").options.add(opt);
opt.text = item;
opt.value = item;
}
function RemoveItemfromListbox()
{
var item = "MyItemtoRemove";
var opt = document.createElement("option");
document.getElementById("Listbox_id).options.remove(opt);
opt.text = item;
opt.value = item;
}