Checkbox validation using JavaScript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Checkbox validation using JavaScript

How do I validate at least one checkbox being checked using JavaScript ?

22nd May 2022, 6:51 AM
Allan Muwangala
7 Answers
0
Have you written your code because without it, it is arduous to get plsss share your code here.
22nd May 2022, 6:59 AM
Davinder Kumar
Davinder Kumar - avatar
0
Jay Matthews Welcome newcomer, here the code you asked for. Next time please try it your own first, share the code if you need help, try to not ask for total solution. https://code.sololearn.com/W749l3ifPLqb/?ref=app
22nd May 2022, 7:47 AM
Calviղ
Calviղ - avatar
0
function Validate(theForm){ var empty = " "; if (theForm.Title.value == "") empty += "Title\n"; if (theForm.Initials.value== "") empty += "Initials\n"; if (theForm.Surname.value == "") empty += "Surname\n"; if (theForm.Email.value == "") empty += "Email\n"; if (theForm.Address.value == "") empty += "Address\n"; if (theForm.hobbies.checked == "") empty += "hobbies\n"; if (theForm.age.value == "") empty += "age\n"; if (theForm.Select_Payment.value == "") empty += "Payment Method\n"; if ( empty !=" "){ alert ("You missed the following essential elements\n" + empty + "Please complete and resubmit"); return false; } }
22nd May 2022, 12:04 PM
Allan Muwangala
0
<select Name= "col"> <option Value=" select"> Select colour </option> <option Value=" bl"> Black </option> <option Value=" GR"> Green </option> <option Value=" yl"> Yellow </option> <option Value=" bt">White </option> <option Value=" re"> Red</option> <option Value=" br"> Brown </option> </Select>
22nd May 2022, 12:04 PM
Allan Muwangala
0
what are your hobbies? Please choose from the list below: <BR><BR> <input type="checkbox" name= "hobbies" value="check"> Football <BR> <input type="checkbox" name= "hobbies" value="check"> Swimming <BR> <input type="checkbox" name= "hobbies" value="check"> Computer games <BR> <input type="checkbox" name= "hobbies" value="check"> Movies <BR> <input type="checkbox" name= "hobbies" value="check"> Driving <BR> <input type="checkbox" name= "hobbies" value="check"> Basketball
22nd May 2022, 12:05 PM
Allan Muwangala
0
I’d like to validate the checkboxes and dropdown list such that if they blank I get a generally alert with the rest of the fields that are not filled.
22nd May 2022, 12:06 PM
Allan Muwangala
0
I can validate them separately but I want on error message for all the empty fields.
22nd May 2022, 12:07 PM
Allan Muwangala