使用javascript

时间:2015-06-17 09:20:27

标签: javascript html css

我正在使用javascript进行简单的表单验证,但它似乎有小故障试图捕获但我无法在这里做的是代码,问题是选择列表没有得到验证我不知道为什么

<html>
<head>
<title>Mobile Phone Prices In Pakistan </title>
<style>
.error{
color:red;

}
</style>


</head>
<body>


<form id="theform" name"form1">
<p>Name:
<input type="text" name="username" id="username" />
<span id="nameerror"></span>
</p>
 <p>
 Email:
 <input type="text" name="email" id="email" />
 <span id="emailerror"></span>
</p>
<p>
Country:
<select name="country" id="country">
<option value="0">choose your country </option>
<option value="pk">pakistan</option>
<option value="ind">India</option>
<option value="afg">afghanistan</option>
<option value="irn">Iran</option>
</select>
<span id="countryerror"></span>
</p>
<p>
Gender
</p>
<p>Male
<input type="radio" name="gender" id="radio" value="radio">
<span id="gendererror"></span>
Femal
<input type="radio" name="gender" id="radio2" value="radio2">
</p>
<p>
<input type="checkbox" name="rules" id="rules">
Accept our rules and regulation to continue with form process 
</p>
<p>
<input type="submit" name="button" id="submit" value="register" >
</p>
</form>



<script type="text/javascript">

document.getElementById("theform").onsubmit = validate;
document.getElementById("submit").disabled = true;
var rules = document.getElementById("rules");
rules.onclick = function (){
    if(rules.checked){
        document.getElementById("submit").disabled = false;

    }else{

        document.getElementById("submit").disabled = true;
    }

}

function validate(){

var username = document.getElementById("username"); 
var email = document.getElementById("email");
var country = document.getElementById("country");       
var radio1 = document.getElementById("radio");  
var radio2 = document.getElementById("radio2"); 
var atpos = email.value.indexOf("@");
var dotpos = email.value.lastIndexOf(".");




if(username.value == "" && username.value.length == 0){

    document.getElementById("nameerror").innerHTML = "please enter your name";
    username.focus();
    document.getElementById("nameerror").className = "error";
    return false;

}else{
    document.getElementById("nameerror").innerHTML = "";
    document.getElementById("nameerror").className= "";
}
if(atpos < 1 || dotpos < atpos+2 || dotpos+2 >= email.vlaue.length){

    document.getElementById("emailerror").innerHTML = "please enter your email";
    email.focus();
    document.getElementById("emailerror").className = "error";
    return false;

}else{
    document.getElementById("emailerror").innerHTML = "";
    document.getElementById("emailerror").className= "";
}
if(country.selectedIndex == 0){
    document.getElementById("countryerror").innerHTML = "please choose country";
    document.getElementById("countryerror").className = "error";
    return false;

}else{
    document.getElementById("countryerror").innerHTML = "";
    document.getElementById("countryerror").className = "";

}

if(radio1.checked == false && radio2.checked == false){

    alert("please choose your gender");
    return false;   
}



}


</script>


</body>
</html>

5 个答案:

答案 0 :(得分:1)

Mistyped here

if(atpos < 1 || dotpos < atpos+2 || dotpos+2 >= email.**vlaue**.length){

答案 1 :(得分:0)

你的if语句中有拼写错误

if(atpos < 1 || dotpos < atpos+2 || dotpos+2 >= email.vlaue.length){

email.vlaue.length 替换为 email.value.length

答案 2 :(得分:0)

原因是,因为您在for node in tree.iter('c1'): branch_coverage = node.attrib.get('percentage') list4.append(branch_coverage) if tree.find('.//c1') is None or len(tree.find('.//c1')) == 0: list4.append("none") 部分拼错了value

使用JSFiddle:http://jsfiddle.net/v4qv7n7m/1/

您的代码:

email

工作代码:

if(atpos < 1 || dotpos < atpos+2 || dotpos+2 >= email.vlaue.length){

    document.getElementById("emailerror").innerHTML = "please enter your email";
    email.focus();
    document.getElementById("emailerror").className = "error";
    return false;

}else{

答案 3 :(得分:0)

错误email.value.length

if(atpos < 1 || dotpos < atpos+2 || dotpos+2 >= email.value.length){
                                                      ^^^^^^

}

http://jsfiddle.net/Ldr3f5pw/

答案 4 :(得分:0)

您实际需要查看的是选择是否有值。

以下是修改后的代码编辑。

<p>Please Select a Security Question from the Drop Down List.<br />
    <select name = "Security Question" id="securityQuestion">
        <option></option>
        <option value="m">What is your Mother's maiden name?</option>
        <option value="p">What is the name of your pet?</option>
        <option value="c">What is your favorite color?</option>
    </select>
    <input type="button" value="Click to Check" onClick="checkDropdown(1)" />
</p>

<script>
    function checkDropdown () {
    var securityQuestionElement = document.getElementById('securityQuestion');
    if(!securityQuestionElement.value) {  
        window.alert('You must select a Security Question');  
        securityQuestionElement.value = 'm'
        return false;  
    }
}
</script>

主要差异

I am checking the select's value, not the option's "checked" status
I am selectingbyId, the browser indexes Ids early on (not sure about by name)
I am checking for the absence of a value, not if the value equals one of 3 cases. (cleaner/easier to read)