开发者

Validating a form with a radio button

UPDATE:

 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> 

i am using bassistance jquery plugin to validate my form. and all other validation works just fine but the radio button is giving the problem, how would i validate radio button, i want the user to select at least one radio button.

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


There are a number of ways to do this.

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

Of course, you're going to want to select by name (just in case you have multiple radio-button sets)

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


Within your "rules" object, you should just be able to do:

gender: "required"

Alternatively, you seem to have the custom attribute:

validate="required:true"

...on your "male" radio. You should be able to add that to both inputs with name="gender" and that should work as well. See the radio/checkbox demo.

If you want to do anything more complex, the plugin gives you the option to create your own validation methods.


here is how i able to solve the problem: hope this help others.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜