简体   繁体   English

通过JavaScript验证单选按钮

[英]Radio button validation through JavaScript

I have the following form: 我有以下表格:

<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>

I am trying to validate whether a Radio button has been selected. 我正在尝试验证是否已选择Radio按钮。

The code I am using: 我正在使用的代码:

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

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

This is not working. 这不起作用。 Any ideas? 有任何想法吗?

When using radiobuttons you have to go through to check if any of them is checked, because javascript threats them as an array: 当使用单选按钮时,你必须检查是否有任何一个被检查,因为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>

Regards 问候

My solution for validation complex forms include radios. 我的验证复杂形式的解决方案包括无线电。

Usage is simple, function return TRUE/FALSE after validation. 用法很简单,验证后函数返回TRUE / FALSE。 var rs_target is ID of form var rs_target是表单的ID

scTo is my custom func to scroll to ID, you can use own function to show/scroll errors scTo是我的自定义功能滚动到ID,你可以使用自己的功能来显示/滚动错误

scTo("#"+err_target);

Error box will be like 错误框就像

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

Validation 验证

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

Function 功能

    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;
    }
}

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

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