通过JavaScript验证单选按钮

时间:2012-11-03 19:46:34

标签: javascript radio-button validation

我有以下表格:

<form name="survey1" action="add5up.php" method="post" onsubmit="return validateForm()">
<div id="question">Q1) My programme meets my expectations</div><br />
Always<INPUT  TYPE = 'Radio' Name ='q1'  value= 'a'> 
Usually<INPUT  TYPE = 'Radio' Name ='q1' value= 'b'> 
Rarely<INPUT  TYPE = 'Radio' Name ='q1' value= 'c'> 
Never<INPUT  TYPE = 'Radio' Name ='q1' value= 'd'> 
<input type="submit" value="addData" />
</form>

我正在尝试验证是否已选择Radio按钮。

我正在使用的代码:

<script type="text/javascript">
function validateForm()
{
if( document.forms["survey1"]["q1"].checked)
{
    return true;
}

else
{
    alert('Please answer all questions');
    return false;
}
}
</script>

这不起作用。有什么想法吗?

2 个答案:

答案 0 :(得分:11)

使用单选按钮时,您必须检查是否有任何选项被检查,因为javascript将它们威胁为数组:

<script type="text/javascript">
function validateRadio (radios)
{
    for (i = 0; i < radios.length; ++ i)
    {
        if (radios [i].checked) return true;
    }
    return false;
}

function validateForm()
{
    if(validateRadio (document.forms["survey1"]["q1"]))
    {
        return true;
    }
    else
    {
        alert('Please answer all questions');
        return false;
    }
}
</script>

此致

答案 1 :(得分:0)

我的验证复杂形式的解决方案包括无线电。

用法很简单,验证后函数返回TRUE / FALSE。 var rs_target是表单的ID

scTo是我的自定义功能滚动到ID,你可以使用自己的功能来显示/滚动错误

scTo("#"+err_target);

错误框就像

<div class="rq_message_box rq_message_box_firstname display-none">err message</div>

验证

    var validation = validateForm(rs_target);
    if(validation == false){
        return false;
    }

功能

    function validateForm(rs_target) {
    var radio_arr = [];
    var my_form = $("#"+rs_target);
    my_form = my_form[0];
    $(".rq_message_box").hide(); //clear all errors
    //console.log(my_form);
    var err = false;
    var err_target = "";

    for (key in my_form) {
        //console.log("do");
        if(!my_form[key]||my_form[key]==null||err){
            break;
        }
            //console.log(my_form[key].name);
                var x = my_form[key].value;
                    //console.log(x);
                    if(my_form[key].type == "radio"){
                        //console.log("radio");
                            if(radio_arr[my_form[key].name] != true){
                                radio_arr[my_form[key].name] = null;
                            }
                            if(my_form[key].checked){
                                radio_arr[my_form[key].name] = true;
                            }
                    }else{
                    if (x == null || x == "") {
                        //console.log(form[key].name.toString() + " must be filled out");
                                err = true;
                                err_target = my_form[key].name;
                        //return false;
                    }
                    }

    }
    //console.log(radio_arr);
    var rad_err = false;
    for (key in radio_arr) {
        if(rad_err){
            break;
        }
        var x = radio_arr[key];
    if (x == null || x == "") {
        //console.log("RADIO> "+key + " must be filled out");
                rad_err = true;
                err_target = key;
    }
    }
    if(err || rad_err){
        // some error stuff, for me show prepared error/help box with class [".rq_message_box_"+err_target] / err_target is name of input like [.rq_message_box_firsname]
        $(".rq_message_box_"+err_target).show(); //show  error message for input
        scTo("#"+err_target); //scroll to - custom func
        return false;
    }else{
        return true;
    }
}