简体   繁体   English

如何验证单选按钮?

[英]How can I vaildate radio buttons?

How i put validation on check box?我如何在复选框上进行验证?

Here is my code:这是我的代码:

<script>
function Validate(){
   var cntct = document.getElementById("contact").value;
   if (cntct.value=="")
   {
      alert("Please select contact medium");
      return false;
   }
}
</script>

<input type="radio" name="contact" id="contact" value="SMS">
<label>SMS</label> <input type="radio" name="contact" id="contact" value="CALL">
<label>CALL</label> <input type="radio" name="contact" id="contact" value="EMAIL">
<label>EMAIL</label>

please help..请帮忙..

Simple: if(cntct == "") {很简单:if(cntct == "") {

} }

The cntct variable is the value! cntct 变量就是值!

in case of checkboxes and radiobuttons, the element is checked when the "checked" property is true:在复选框和单选按钮的情况下,当“checked”属性为真时检查元素:

function Validate(){
    var selectedValue = "";
    var cntct = document.getElementsByname("contact");
    for(var i=0;i<cntct.length;i++) {
        if (cntct[i].checked)
        {
            selectedValue = cntct.value;
        }
    }
    if(selectedValue == "") {
        alert("Please select contact medium");
        return false;
    }
    return true;
}

UPDATE更新

I just noticed that you have multiple elements with the same ID.我刚刚注意到您有多个具有相同 ID 的元素。 This is not allowed, in such case the browser will return only the last element with that ID.这是不允许的,在这种情况下,浏览器将只返回具有该 ID 的最后一个元素。 You need to iterate over the elements with the same name and pick one with attribute checked .您需要遍历具有相同name的元素并选择一个属性为checked的元素。 See the code above见上面的代码

How i put validation on check box?我如何在复选框上进行验证?

I presume you mean "how do I make sure one of the radio buttons is checked when the form is submitted" (supposing there is a form you haven't shown).我想您的意思是“我如何确保在提交表单时检查其中一个单选按钮”(假设有一个您没有显示的表单)。

The simple way is to make one checkbox selected by default, then you know that one will always be selected and you don't have to check, eg简单的方法是默认选中一个复选框,然后您知道将始终选中一个并且您不必检查,例如

<input type="radio" name="contact" value="CALL" checked>
<input type="radio" name="contact" value="EMAIL">

The other way is to loop over the radio buttons when the form is submitted (or whatever event you decide to base the validation on) and make sure one is checked.另一种方法是在提交表单(或您决定基于验证的任何事件)时循环单选按钮并确保选中一个。 eg例如

function checkButton() {
  var nodes = document.getElementsByName('contact');
  for (var i=0, iLen=nodes.length; i<iLen; i++) {

    // If find a checked one, job's done
    if (nodes[i].checked) return true;
  }

  // Otherwise, none were checked
  return false;
}

There is an input type text with a name of contact, but it won't have a checked property so will be treated like an unchecked radio.有一个输入类型文本,其名称为联系人,但它没有选中的属性,因此将被视为未选中的收音机。

Please try this:请试试这个:

<script>
function SubmitIt()
{
    if (document.forms.myform.elements.contact.value == "") 
    {
        alert("Please select contact medium..."); 
    }
}
</script>

<form name="myform" id="myform">
   <input type="radio" name="contact" id="contact" value="SMS">
   <label>SMS</label> <input type="radio" name="contact" id="contact" value="CALL">
   <label>CALL</label> <input type="radio" name="contact" id="contact" value="EMAIL">
   <label>EMAIL</label>

   <button onclick="SubmitIt();">Submit</button>
</form>

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

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