mercredi 21 septembre 2016

Javascript validation for checkbox in sequence

first I'm sorry for my bad english language. I'm new with Javascript and I had a problem. My problems are about checkbox validation using javascript, so the first checkbox that I have should checked or show an alert (I have done with this problem, so there's no more problem about it), but the second problem is the checkbox should checked in sequence (or order) for the example there are 4 checkbox, I checked the first, second, and the fourth (not in sequence/order), so there's an alert, but if I checked the first, second, and third, it's good. Does anyone know how to validate with that condition with javascript?

Here's my code

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>

</head>

<body>
<form action="form-si-hasil.php" method="post" onsubmit="return validate(this)" id="form" name="form">
<h1>Form Hobi</h1>
<input type="checkbox" name="ck1" value="Membaca" id="checkbox1">Membaca
<br />
<input type="checkbox" name="ck2" value="Sport" id="checkbox2">Sport
<br />
<input type="checkbox" name="ck3" value="Singing" id="checkbox3">Singing
<br />
<input type="checkbox" name="ck4" value="Dancing" id="checkbox4">Dancing
<br />
<br />
<input type="submit">
</form>
<script src="../js/jquery-1.11.2.min.js"></script>
<script type="text/javascript" language="JavaScript">
function validate(){
    var pilihan1 = document.getElementById("checkbox1").checked;

    if(pilihan1=="") {
        alert("Checkbox pertama tidak boleh kosong");
    return false
} 
    return true
}
</script> 
</body>
</html>




Aucun commentaire:

Enregistrer un commentaire