rodrigosantosbr

[JS] Please Select a checkbox

Jan 17th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!

http://jsbin.com/?html,js,output


HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<form name="form" onsubmit="return Valida(this)">
<input type="checkbox" value="1" id="chk" name="item1"> Item 1
<input type="checkbox" value="2" id="chk" name="item2"> Item 2
<input type="checkbox" value="3" id="chk" name="item3"> Item 3
<input type="checkbox" value="4" id="chk" name="item4"> Item 4
<input type="checkbox" value="5" id="chk" name="item5"> Item 5
<br><br>
<input type="submit" value="Enviar">
</form>
</body>
</html>

Javascript

function Valida(form){
var ok = false;
var form = document.forms[0].chk;

for (i = 0; i < form.length; i++)   { 
    if (form[i].checked) { 
        ok = true; 
    }
}
if (!ok) {
    alert ("Selecione algum item.");
    return false;
}

}

Add Comment
Please, Sign In to add comment