In: Computer Science
Create a JavaScript function that will collect the information from the form and verify that it is the correct type and that there are no blank textboxes.
Save and test the file to ensure that the textbox information is collected and the script is working correctly.
Use the onclick event within the submit button to call this function.
Output validation error messages by writing directly to the
with the id of "results."(You may want to use alert boxes for testing at first. If you do this, remove the alerts and ensure the message is displayed correctly before submitting.)
Create a loop within the above function that collects the checked value of the radio button group.
Once the value from the radio group is collected,
create a function to pass the value for evaluation and return a message for the user based on his or her selected skill type using the
with the id of "more."Please I need the same result like as in the image you can copy the link to download the image and below is my code fix that thank you!!!
https://devryu.instructure.com/courses/62607/files/9495251/preview
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JavaScript Exercises</title>
<style type="text/css">
body {
font-family:Verdana, Geneva, sans-serif;
font-size:100%;
margin:0;
padding:0;
}
p {
color:#900;
margin-left:20px;
}
div#results {
background-color:#FF6;
height:auto;
width:500px;
border:1px solid red;
padding:10px;
margin-left:20px;
-moz-box-shadow: 10px 10px 5px #888;
-webkit-box-shadow: 10px 10px 5px #888;
box-shadow: 10px 10px 5px #888;
border-radius:7px;
}
div#more {
background-color: #39F;
height:auto;
width:500px;
border:1px solid #036;
padding:10px;
margin-left:20px;
margin-top:20px;
-moz-box-shadow: 10px 10px 5px #888;
-webkit-box-shadow: 10px 10px 5px #888;
box-shadow: 10px 10px 5px #888;
color:#CF0;
}
#form1
{
padding: 10px;
width: 500px;
border-style: solid;
border-color: #063;
border-radius: 15px;
-moz-box-shadow: 10px 10px 5px #888;
-webkit-box-shadow: 10px 10px 5px #888;
box-shadow: 10px 10px 5px #888;
background-color: #CFF;
margin:20px;
}
</style>
<script type="text/javascript">
function validateForm(){
var name = document.getElementById("name").value;
var age = document.getElementById("age").value;
if(name == "" || name == null){//checking for null or empty
string
resultsMsg("Hey, you forgot to fill in your name!");
}else{
if(age == "" || age == null || isNaN(age)){
resultsMsg("Age is required!");
}else{
if(!getSkill()){
resultsMsg("Please select a skill");
}else{
resultsMsg("Success, you selected " + getSkill());
}// end else
}
}//end else
}//end function
function getSkill(){
var isChecked = false; // assume no button is checked
var theSelection;
var skills = document.getElementsByName('skillset');// returns an
array
for (var i=0; i < skills.length; i++){
if(skills[i].checked){
isChecked = true;
theSelection = skills[i].value;
break; //leave the loop since only one can be checked
}// end if
}// end for
if(isChecked){
return theSelection;
}else{
return false;
}//end else
}// end function
function resultsMsg(S){
var resultsBox = document.getElementById("results");
//reset to blank by overwriting
resultsBox.innerHTML= S;
}//end function
</script>
</head>
<body>
<p>First Paragraph</p>
<p>Second Paragraph</p>
<form name="form1" id="form1" action="" method="post">
<label>First Name:
<input type="text" id="name" name="name">
</label>
<br> <!-- new line here -->
<label>Your Age:
<input type="text" id="age" name="age">
</label>
<br> <!-- new line here -->
<input type="radio" name="skillset"
value="Designer">Designer<br>
<input type="radio" name="skillset"
value="Developer">Developer<br>
<input type="radio" name="skillset"
value="Programmer">Programmer<br>
<input type="radio" name="skillset"
value="Artist">Artist<br>
<input type="button" value="Submit"
onclick="validateForm();"> <input
type="reset" value="Clear Form">
</form>
<div id="results"></div>
<div id="more">You did not selected a Skill</div>
</body>
</html>
Please upvote if you are able to understand this and if there is any query do mention it in the comment section.
CODE:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JavaScript Exercises</title>
<style type="text/css">
body {
font-family:Verdana, Geneva, sans-serif;
font-size:100%;
margin:0;
padding:0;
}
p {
color:#900;
margin-left:20px;
}
div#results {
background-color:#FF6;
height:auto;
width:500px;
border:1px solid red;
padding:10px;
margin-left:20px;
-moz-box-shadow: 10px 10px 5px #888;
-webkit-box-shadow: 10px 10px 5px #888;
box-shadow: 10px 10px 5px #888;
border-radius:7px;
}
div#more {
background-color: #39F;
height:auto;
width:500px;
border:1px solid #036;
padding:10px;
margin-left:20px;
margin-top:20px;
-moz-box-shadow: 10px 10px 5px #888;
-webkit-box-shadow: 10px 10px 5px #888;
box-shadow: 10px 10px 5px #888;
color:#CF0;
}
#form1
{
padding: 10px;
width: 500px;
border-style: solid;
border-color: #063;
border-radius: 15px;
-moz-box-shadow: 10px 10px 5px #888;
-webkit-box-shadow: 10px 10px 5px #888;
box-shadow: 10px 10px 5px #888;
background-color: #CFF;
margin:20px;
}
</style>
<script type="text/javascript">
function validateForm(){
//fetching the respective textboxes from their ids
var name = document.getElementById("name").value;
var age = document.getElementById("age").value;
if(name == "" || name == null){//checking for null or empty string
resultsMsg("Hey, you forgot to fill in your name!");
} else if (!isNaN(name)) {//if name is a number
resultsMsg("Name is invalid!")
} else {
if(age == "" || age == null /* || isNaN(age) */){
resultsMsg("Age is required!");
} else if (isNaN(age)) {//checking if age is not a number
resultsMsg("Age is invalid!");
} else {
if(!getSkill()){//if there is no skill selected
resultsMsg("Please select a skill");
} else {
resultsMsg("Success, you selected " + getSkill());
}// end else
}
}//end else
}//end function
function getSkill() {
var isChecked = false; // assume no button is checked
var theSelection;
var skills = document.getElementsByName('skillset');// returns an array
for (var i=0; i < skills.length; i++) {//looping through all skills
if(skills[i].checked) {
isChecked = true;
theSelection = skills[i].value;//the one selected by the user
break; //leave the loop since only one can be checked
}// end if
}// end for
if(isChecked) {
return theSelection;//returning the selected skill
} else {
return resultsMsg("You did not selected a skill");
}//end else
}// end function
function resultsMsg(S){
var resultsBox = document.getElementById("results");//reset to blank by overwriting
resultsBox.innerHTML= S;
}//end function
</script>
</head>
<body>
<p>First Paragraph</p>
<p>Second Paragraph</p>
<form name="form1" id="form1" action="" method="post">
<label>First Name:
<input type="text" id="name" name="name">
</label>
<br> <!-- new line here -->
<label>Your Age:
<input type="text" id="age" name="age">
</label>
<br> <!-- new line here -->
<!-- creating all the skills with radio buttons -->
<input type="radio" name="skillset" value="Designer">Designer<br>
<input type="radio" name="skillset" value="Developer">Developer<br>
<input type="radio" name="skillset" value="Programmer">Programmer<br>
<input type="radio" name="skillset" value="Artist">Artist<br>
<input type="button" value="Submit" onclick="validateForm();"> <input type="reset" value="Clear Form">
</form>
<div id="results"></div>
<!-- <div id="more">You did not selected a Skill</div> -->
</body>
</html>
OUTPUT:
If this was supposed to be done in any other way or there is still any change required in this then plese mention it in the comment section otherwise please upvote.