简体   繁体   English

使用Javascript验证单选按钮

[英]Validation of radio buttons with Javascript

I've combed a ton of the pages on here, and still am incapable to get my explicit validator to work. 我已经在这里梳理了大量的页面,但仍然无法让我的显式验证器工作。 Basically, when the submit button is clicked, I want the script to verify a radio is checked, if one is checked to do nothing. 基本上,当单击提交按钮时,我希望脚本验证是否检查了无线电,如果检查了无线电。 If one isn't checked I want it to post an alert message. 如果未选中,我希望它发布警报消息。

roughly my html looks like: 大致我的HTML看起来像:

<form id="myForm" onsubmit = "check()">
<input type = "radio" name = "c" id = "1" value = "1" />
<input type = "radio" name = "c" id = "2" value = "2" />
<input type = "radio" name = "c" id = "3" value = "3" />

<input type = "submit" value = "Submit" />

my JS page looks like: 我的JS页面看起来像:

function check() {
    var r = document.getElementsByName("c")
    var c = 0

    for(var i=0; i < r.length; i++){
       if(c[i].checked) {
          c = i; }
    }

    alert("please select radio");
}

try this one 试试这个

function check() {
var r = document.getElementsByName("c")
var c = -1

for(var i=0; i < r.length; i++){
   if(r[i].checked) {
      c = i; 
   }
}
if (c == -1) alert("please select radio");
}

this 这个

c[i].check

should be 应该

c[i].checked

and you're not actually doing anything with the result, you're just always alerting. 而你实际上并没有对结果做任何事情,你只是总是提醒你。

<html>
<head>
<script language="javascript">
function check()    {

chosen = ""
len = document.myform.chk.length

for (i = 0; i <len; i++) {
if (document.myform.chk[i].checked) {
chosen = document.myform.chk[i].value
}
}

if (chosen == "") {
alert("No Option selected");
return false;
}
else {
alert("option selected");
return true;
}
}
</script>
</head>
<body>
<form name="myform" onsubmit = "return check();">
<input type = "radio" name = "chk" id = "1" value = "1" >
<input type = "radio" name = "chk" id = "2" value = "2" >
<input type = "radio" name = "chk" id = "3" value = "3" >

<input type="submit" value="submit">

</form>
</body>
</html>

Check for this validation hope its may also help you . 检查此验证希望它也可以帮助您。 Also check it in jsfiddle 还可以在jsfiddle查看

             function ShowMsg() {    

             if (fnSpeciality() == false) 
              {
              document.getElementById("myform").focus();
               return false;
            }

                function fnSpeciality() 
                {
             return fnRblfnSpeciality();
                 }
              function fnSpeciality() {
                   return fnRblfnSpeciality();
                          }


    function fnRblfnSpeciality() {

        var list = document.getElementById('myform'); //Client ID of the radiolist
        var inputs = list.getElementsByTagName("input");
        var isItemChecked = false;
        for (var i = 0; i < inputs.length; i++) {
            var listItem = inputs[i];

            if (listItem.checked) {
                //alert(listItem.value);
                isItemChecked = true;
                break;
            }
        }
     if (isItemChecked == false) {
            if (isItemChecked =="") {
                alert('Please select a speciality.');
                return false;
            }
            // else return true;
        }
        return true;
    }

CreditCard DebitCard By Cash CreditCard DebitCard现金

                </td>
            </tr>

Am using radio button list ..pls share if have java script validation for this radio button 使用单选按钮列表..pls共享如果有此单选按钮的java脚本验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM