在javascript中验证复选框

时间:2014-04-19 21:40:21

标签: javascript jquery validation

您是Javascript的新手并且不知道如何验证我的复选框我已查找了许多示例但仍然不明白它可以请某人告诉我如何验证它以及我的下拉菜单inst验证在任何提前帮助将不胜感激。

My Java Script and HTML

function validateForm(){

    var fname, lname, sex, address, email, length, songs, a, i, check, error;

    a=0;
    check=false;
    error=false;

    fname=document.getElementById("firstname").value;
    lname=document.getElementById("lastname").value;

    sex=document.getElementsByName("sex");

    address=document.getElementById("address").value;
    email=document.getElementById("email").value;
    length=document.getElementById("len").value;

//  songs=document.getElementByName("f_song");

    if(fname=="" || fname==null){

        alert("please input a first name");
        error=true;
        return false;
    }

    if(lname=="" || lname==null){

        alert("please input your last name");
        error=true;
        return false;
    }

    for(i=0; i<sex.length; i++){

        if(sex.item(i).checked==false){

                a++;
        }
    }

    if(a==sex.length){

            alert("Please select Male or Female");
            error=true;
            return false;

    }

    if(address=="" || address==null){

        alert("Please input your address thanks");
        error=true;
        return false;


    }

    if(email=="" || email==null){

        alert("Please enter a email address");
        error=true;
        return false;
    }

    if(length=="" || length==null){

        alert("Be select how long have you been a fan");
        error=true;
        return false;
    }






    alert("Am working ");

}


MY HTML


<head>
<title>] </title>
<link rel="stylesheet" type="Text/css" href="wit_frm.css"/>

<script type="text/javascript" src="java.js">

    </script>
</head>

<body>

    <div id="container">

        <div id="header">

        </div>

        <div id="topnav">

            <div id="link">
                <a href="wit.html">Home</a>
                <a href="http://www.whitneyhouston.com">Fan Site</a>
                <a href="wit_form.html">Register</a>
            </div>

        </div>



        <div id="contentarea">

            <form method="post" action="" onsubmit= "return validateForm();">

            <fieldset>
            <legend>Personal Information</legend>

            First Name:<input type="text" name="fname" id="firstname"/>
            <br/>
            <br/> Last Name:<input type="text" name="lname" id="lastname"/>
            <br/>
            <br/> Sex:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  <input type="radio" name="sex" id="m"/>Male  <input type="radio" name="sex" id="f"/>Female
            <br/>
            <br/>Address:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<textarea id="address" name="add" rows="4" cols="30"></textarea>

            </fieldset>

            <fieldset>
            <legend> User Information</legend>
            <br/>Email: &nbsp;  &nbsp; &nbsp;  &nbsp;  &nbsp; <input type="text" name="e_address" id="email"/>
            <br/>
            <br/>No of years listening  to her music: <select name="n_years" id="len">
                                                <option value="less than a yr"> less than 1yr</option>
                                                <option value="2-5 years">2-5 years</option>
                                                <option value="5+ years">5+ years</option>
                                                </select>
            <br/>                                   
            <br/>Favorite Songs:I will always love you<input type="checkbox" name="f_song" value="I will always love you"/> I look to you<input type="checkbox" name="f_song" value="I look to you"/></p>  
                            <br/> &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; &nbsp;  &nbsp;  &nbsp;  &nbsp;  I have nothing<input type="checkbox" name="f_song" value="I have nothing"/> One moment in time<input type="checkbox" name="f_song" value="one moment in time"/></p>
                            <br/>&nbsp;  &nbsp;  &nbsp;  &nbsp; &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; I wanna dance with somebody<input type="checkbox" name="f_song" value="I wanna dance with somebody"/> Greatest Love of All<input type="checkbox" name="f_song" value="Greatest love of all"/></p>

            </fieldset>
            <br/><input type="submit" name="sub_button" value="SUBMIT"/> &nbsp;  &nbsp;  &nbsp; <input type="reset" name="res_button" value="CLEAR"/>

            </form>



        </div>

        <div id="footerfix">

        </div>

        <div id="footer">
             <div id="link_1">
                <a href="wit.html">Home</a>
                <a href="">Fan Site</a>
                <a href="">About Her</a>
            </div>
        </div>

    </div>




</body>
</html>

3 个答案:

答案 0 :(得分:1)

我会这样做:

Bin example

var f_song = document.getElementsByName("f_song");
var oneSongSelected = 0;
//Loop through the checkbox elements, and tally the selected
for(var ss = 0; ss < f_song.length; ss++){
  if(f_song[ss].checked){
    oneSongSelected++;
  }
}
//If none are selected, then alert your error, and return false
if(oneSongSelected === 0){
  alert("Please select at least one song");
  return false;
}

答案 1 :(得分:0)

由于您是JavaScript新手,我建议您学习jQuery,它比JavaScript更简单,更简单。如果您有复选框并且想验证它们,可以试试这个:

$('#checkbox').click(function(){
    if (this.checked) {

    }
});

代码说明

$('#checkbox').click(function() =&gt;这行代表&#34;当点击id #checkbox的元素时,执行此函数内的内容。&#34;您需要将#checkbox替换为复选框的ID(例如:#yourCheck)或其类(例如:.yourClass)。

if (this.checked) =&gt;此if语句检查元素是否已选中。您应该在括号内选中复选框时包含您的代码。

编辑:这是我制作的JSFiddle example

答案 2 :(得分:0)

你可以使用jquery,但你可以这样做:

在您的html代码中,您可以使用第一个下拉选项&#34;选择&#34;和价值&#34; -1&#34;:

<select id="dropDown">
    <option value="-1">Select</option>
</select>

复选框:

<input type="checkbox" name="checkbox" id="checkbox"  />

DropDown JavaScript:

var aValue;
aValue = document.getElementById("dropDown").value;

if(aValue == "-1"){
    return false;
}

CheckBox JavaScript:

var checkbox = document.getElementById('checkbox');

if(!checkbox.checked){
    return false;
}