In: Computer Science
How to validate Javascript form data?
Here is the code. Can someone modify it so that all the information is validated? Thanks.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Music Survey</title>
<meta charset="utf-8">
</head>
<style>
legend { font-weight:bold;
}
</style>
<body>
<h1>Music Survey</h1>
<form method="post" action="">
<label
for="myname"><b>Name:</b></label>
<input type="text" name="myname"
id="myname">
<br><br>
<label
for="myemail"><b>Email:</b></label>
<input type="email"
name="myemail" id="myemail">
<br><br>
<fieldset>
<legend>Select Your Favorite Types of
Music:</legend>
<input type="checkbox" id="pop" /> Pop <br>
<input type="checkbox" id="classic" /> Classical
<br>
<input type="checkbox" id="rock" /> Rock <br>
<input type="checkbox" id="folk" /> Folk <br>
<input type="checkbox" id="rap" /> Rap <br>
<input type="checkbox" id="other" /> Other <br>
</fieldset>
<br><br>
<fieldset>
<legend>Select How Often You Purchase Music
CDs:</legend>
<input type="radio" name="musicCD" value="weekly" /> Weekly
<br>
<input type="radio" name="musicCD" value="fewCDs" /> A few
CDs each year <br>
<input type="radio" name="musicCD" value="monthly" /> Monthly
<br>
<input type="radio" name="musicCD" value="never" /> Never
purchase <br>
</fieldset>
<br><br>
<b>Select the Locations You Listen to
Music:</b>
<br>
<select id="listenMusic" multiple>
<option value="home" selected>At home</option>
<option value="inTheCar">In the car</option>
<option value="anywhere">Anywhere</option>
</select>
<br><br>
<b>What Role Does Music Play in Your Life?</b>
<br>
<textarea id="sports" rows="4" cols="60"
maxlength="60"></textarea>
<br>
<input type="submit" value="Submit" />
<input type="reset" value="Reset" /> <br>
</form>
</body>
</html>
Here is code:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Music Survey</title>
<meta charset="utf-8">
</head>
<style>
legend {
font-weight: bold;
}
</style>
<script>
function validate(form) {
if (form.myname.value.length == 0) {
alert("Please enter your first name.");
return false;
} else if (form.myemail.value.length == 0) {
alert("Please enter your email name.");
return false;
} else if (validateCheckBox(form) == false) {
alert("Please choose your Favorite Types of Music");
return false;
} else if (validateRadio(form) == false) {
alert("Please choose How Often You Purchase Music CDs");
return false;
} else if (document.getElementById("listenMusic").value.length ==
0) {
alert("Please choose the Locations You Listen to Music.");
return false;
} else if (document.getElementById("sports").value.length == 0)
{
alert("Please enter your Music Play in Your Life.");
return false;
}
alert(
"Thank You."
);
return;
}
function validateRadio(form) {
if (
(form.musicCD[0].checked == false) &&
(form.musicCD[1].checked == false) &&
(form.musicCD[2].checked == false) &&
(form.musicCD[3].checked == false)
) {
return false;
}
return true;
}
function validateCheckBox() {
return (document.getElementById("pop").checked ||
document.getElementById("classic").checked ||
document.getElementById("rock").checked ||
document.getElementById("folk").checked ||
document.getElementById("rap").checked ||
document.getElementById("other").checked)
}
</script>
<body>
<h1>Music Survey</h1>
<form name="form" method="post" action="" onsubmit="return
validate(form)">
<label
for="myname"><b>Name:</b></label>
<input type="text" name="myname" id="myname">
<br><br>
<label
for="myemail"><b>Email:</b></label>
<input type="email" name="myemail" id="myemail">
<br><br>
<fieldset>
<legend>Select Your Favorite Types of
Music:</legend>
<input type="checkbox" id="pop" /> Pop <br>
<input type="checkbox" id="classic" /> Classical
<br>
<input type="checkbox" id="rock" /> Rock <br>
<input type="checkbox" id="folk" /> Folk <br>
<input type="checkbox" id="rap" /> Rap <br>
<input type="checkbox" id="other" /> Other <br>
</fieldset>
<br><br>
<fieldset>
<legend>Select How Often You Purchase Music
CDs:</legend>
<input type="radio" name="musicCD" value="weekly" /> Weekly
<br>
<input type="radio" name="musicCD" value="fewCDs" /> A few
CDs each year <br>
<input type="radio" name="musicCD" value="monthly" /> Monthly
<br>
<input type="radio" name="musicCD" value="never" /> Never
purchase <br>
</fieldset>
<br><br>
<b>Select the Locations You Listen to
Music:</b>
<br>
<select id="listenMusic" multiple>
<option value="home" selected>At home</option>
<option value="inTheCar">In the car</option>
<option value="anywhere">Anywhere</option>
</select>
<br><br>
<b>What Role Does Music Play in Your Life?</b>
<br>
<textarea id="sports" rows="4" cols="60"
maxlength="60"></textarea>
<br>
<input type="submit" value="Submit" />
<input type="reset" value="Reset" /> <br>
</form>
</body>
Output: