使用单选按钮验证表单

时间:2010-11-09 20:33:12

标签: jquery validation

更新

 rules: {
                    firstname: "asdfsadf",
                    lastname: "123123",
                    city: "required",
                    state: "required",
                    country: "required" ,
                    question_1: "required" 


                },
                messages: {
                    firstname: "Please enter your firstname",
                    lastname: "Please enter your lastname", 
                    city: "Please enter a valid email address",
                    country: "Please accept our policy",
                    question_1: "SELECT at least one question."
                }

    <div class="question"> 
        Q1: What is the second letter of the alphabet? 
        <div class="choices"> 
            <input type="radio" name="question_1" value="1" /> A 
            <input type="radio" name="question_1" value="2" /> B 
            <input type="radio" name="question_1" value="3" /> C 
        </div> 
    </div> 
    <div class="question"> 
        Q2: Which out of these is a berry? 
        <div class="choices"> 
            <input type="radio" name="question_2" value="1" /> Apples 
            <input type="radio" name="question_2" value="2" /> Bananas 
            <input type="radio" name="question_2" value="3" /> Carrots 
        </div> 
    </div> 

我正在使用bassistance jquery插件来验证我的表单。所有其他验证工作正常,但单选按钮给出了问题,如何验证单选按钮,我希望用户至少选择一个单选按钮。

<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery.validate.min.js" type="text/javascript"></script>


<script type="text/javascript">

        $(document).ready(function () { 

            // validate the comment form when it is submitted  
            // validate signup form on keyup and submit
            $("#signupForm").validate({
                rules: {
                    firstname: "asdfsadf",
                    lastname: "123123",
                    city: "required",
                    state: "required",
                    country: "required"  
                },
                messages: {
                    firstname: "Please enter your firstname",
                    lastname: "Please enter your lastname", 
                    city: "Please enter a valid email address",
                    country: "Please accept our policy"
                }
            });  
        });

<form class="cmxform" id="form1" method="get" action="">
    <fieldset>
        <legend>Validating a form with a radio and checkbox buttons</legend>
        <fieldset>
            <legend>Gender</legend>
            <label for="gender_male">
                <input  type="radio" id="gender_male" value="m" name="gender" validate="required:true" />
                Male
            </label>
            <label for="gender_female">
                <input  type="radio" id="gender_female" value="f" name="gender"/>
                Female
            </label>
            <label for="gender" class="error">Please select your gender</label>
        </fieldset>

        <p>
            <input class="submit" type="submit" value="Submit"/>
        </p>
    </fieldset>
</form>

3 个答案:

答案 0 :(得分:1)

有很多方法可以做到这一点。

if ($('input:radio:checked').length)
{
 at least one is checked
}
else
{
 none are checked
}

当然,您需要按名称进行选择(以防您有多个单选按钮组)

if ($('input[name="yourNameHere"]:checked').length)

答案 1 :(得分:0)

在“规则”对象中,您应该能够:

gender: "required"

或者,您似乎拥有自定义属性:

validate="required:true"

...在你的“男性”收音机上。您应该能够将其添加到名称=“性别”的两个输入,并且这也应该有效。 See the radio/checkbox demo

如果您想做更复杂的事情,该插件可让您选择创建自己的验证方法。

答案 2 :(得分:-1)

这是我如何解决问题:希望这有助于他人。

<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery.validate.min.js" type="text/javascript"></script>

<div class="question">  
        Q1: What is the second letter of the alphabet?  
        <div class="choices">  
            <input type="radio" name="question_1" value="1" class="required" /> A  
            <input type="radio" name="question_1" value="2" /> B  
            <input type="radio" name="question_1" value="3" /> C  
            <label for="question" class="error">Please select your at leat one question</label>
        </div>  
    </div>