0% found this document useful (0 votes)
29 views

for Radio Elements: Function If Var Var Var Var Var Var For Var If Break

This document contains JavaScript code to validate a form. It gets the values of various form fields like name, email, gender, subjects, and country. It then checks if the terms and conditions checkbox is checked. If checked, it displays an alert with the submitted details. If not checked, it displays an alert to agree to terms and conditions and focuses on the checkbox.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

for Radio Elements: Function If Var Var Var Var Var Var For Var If Break

This document contains JavaScript code to validate a form. It gets the values of various form fields like name, email, gender, subjects, and country. It then checks if the terms and conditions checkbox is checked. If checked, it displays an alert with the submitted details. If not checked, it displays an alert to agree to terms and conditions and focuses on the checkbox.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

1

2 document.getElementById("form1").onsubmit=validateForm;
3
4 function validateForm(){
5
6 if(document.getElementById("ab").checked){
7
8 var fnam=document.getElementById("fname").value;
9 var lnam=document.getElementById("lname").value;
10 var email=document.getElementById("email").value;
11
12 //for radio elements
13 var radios = document.getElementsByName('gender');
14 var l=radios.length;
15 var gen;
16 for (var i = 0; i < l; i++) {
17 if (radios[i].checked) {
18
19 gen=radios[i].value;
20 break;
21 }
22 }
23 //alert("gen="+gen);
24
25 //for country
26 var country=document.getElementById("c1").value;
27
28 //for subjects
29 var subjects = document.getElementsByName('subject');
30 var l=subjects.length;
31 var sub=[];
32 var j=0;
33 for (var i = 0; i < l; i++) {
34 if (subjects[i].checked) {
35
36 sub[j]=subjects[i].value;
37 j++;
38 }
39 }
40 //alert("sub="+sub);
41
42 alert("Your details\n"+"Full Name:"+fnam+"
"+lnam+"\nEmail:"+email+"\nGender:"+gen+"\nSubjects:"+sub+
43 "\nCountry:"+country+"\n Thank you!")
44 return false;
45 }
46 else{
47 alert("please agree terms of condition");
48 document.getElementById("ab").focus();
49 document.getElementById("ab").select();
50 return false;
51 }
52 }

You might also like