Javascript表单验证工作不正常

Javascript表单验证工作不正常,javascript,Javascript,JS工作不正常。我不知道为什么。有人能帮我吗?这是我的密码 function validate() { if(document.contactform.name.value==''){ alert('Fill the Input name'); name.focus(); return false; } if(document.contactform.email.value==''){ alert('Fill

JS工作不正常。我不知道为什么。有人能帮我吗?这是我的密码

function validate() {
    if(document.contactform.name.value==''){
        alert('Fill the Input name');
        name.focus();
        return false;
    }
    if(document.contactform.email.value==''){
        alert('Fill the Input email');
        email.focus();
        return false;
    }
    if(document.contactform.email.value!=''){
        if(!checkEmail(email.value)){
            alert('Please specify your correct Email!');
            email.focus();
            return false;           
        }       
    }
    if(document.contactform.mobile.value==''){
        alert('Fill the Input mobile');
        mobile.focus();
        return false;
    }
    if(document.contactform.mobile.value!=''){
        if(!IsNumeric(mobile.value)){
            alert('Please specify your correct Mobile Number!');
            mobile.focus();
            return false;           
        }       
    }
    if(document.contactform.subject.value==''){
        alert('Fill the Input Subject');
        subject.focus();
        return false;
    }   
    if(document.contactform.message.value==''){
        alert('Fill the Input description');
        message.focus();
        return false;
    }   
    if(!document.contactform.agree.checked){
        alert('Please check the terms and conditions'); 
        return false; 
    } 
    else{
        return true;
    }
}
这是我的html

<form name="contactform" id="form" class="form" action="newmail.php" onsubmit="return validate();" method="post">
<TABLE align="center"  border="0">
 <TR><TD align="right"> <b>Name :</b></TD><TD align="left"><input type="text" name="name" id="name" /></TD></TR>
 <TR><TD align="right"> <b>Email :</b></TD><TD align="left"><input type="text" name="email" id="email" /></TD></TR> 
 <TR><TD align="right"> <b>Mobile :</b></TD><TD align="left"><input type="text" name="mobile" id="mobile" /></TD></TR> 
 <TR><TD align="right"> <b>subject :</b></TD><TD align="left"><input type="text" name="subject" id="subject" /></TD></TR> 
 <TR><TD align="right"> <b>Message :</b></TD><TD align="left"><textarea name='message' id='message'></textarea></TD></TR>
 <TR><TD colspan="2" align="center"><label for="agree"><input type="checkbox" name="agree" id="agree" checked="checked"> I agree to terms and Conditions</label> </TD></TR> 
 <TR><TD colspan="2" align="center"><input type="submit" value="Submit" class="submit" /> </TD></TR>
</TABLE>
</form>

姓名:
电邮:
流动电话:
主题:
信息:
我同意条款和条件
代码不能单独用于名称字段。如果我对文件中的姓名代码进行注释,效果很好。有什么不对吗??在我的另一种形式中,textarea字段本身不起作用。在此表单消息字段中,即文本区域验证正在工作

事情就是这样。当我提交表单时,若名称字段为空,它将显示警报并直接进入目标页面。如果我对代码进行注释以进行名称验证,那么其余的代码可以通过警告相关错误来正常工作。

您的表单元素有一个
name
属性

您不能有一个名为
name
input
元素

document.contactform.name
是否指的是表单名称或您调用的
name
输入


将您的输入元素更改为其他元素-
fullname
,例如,在javascript中使用该元素,您应该可以。您的变量:
name
email
mobile
subject
agree
都未定义。除了IE从ID为的元素创建全局变量,但您不应该使用它。有些浏览器不会在它上面创建全局视图。以下是我将如何编写您的脚本

function validate() {
    var name = document.getElementById('name');
    if(!name.value){
        alert('Fill the Input name');
        name.focus();
        return false;
    }

    var email = document.getElementById('email');
    if(!email.value){
        alert('Fill the Input email');
        email.focus();
        return false;
    }

    // No need to check if (email.value) again
    if(!checkEmail(email.value)){
        alert('Please specify your correct Email!');
        email.focus();
        return false;
    }

    var mobile = document.getElementById('mobile');
    if(!mobile.value){
        alert('Fill the Input mobile');
        mobile.focus();
        return false;
    }

    //No need to check if mobile.value again

    if(!IsNumeric(mobile.value)){
        alert('Please specify your correct Mobile Number!');
        mobile.focus();
        return false;
    }

    var subject = document.getElementById('subject');
    if(!subject.value){
        alert('Fill the Input Subject');
        subject.focus();
        return false;
    }

    var message = document.getElementById('message');
    if(!message.value){
        alert('Fill the Input description');
        message.focus();
        return false;
    }

    var agree = document.getElementById('agree');
    if(!agree.checked){
        alert('Please check the terms and conditions');
        return false;
    }

    // No need for an else here
    return true;
}

当且仅当元素与另一个属性的名称不匹配时,通过表单对象的属性引用表单字段才有效

“name”是表单的属性,因此
document.contactform.name.value
实际上指的是联系人表单的“name”属性,而不是名为“name”的表单元素


请将您的字段重命名为其他名称,如“全名”,或者更好地使用ids和document.getElementById访问表单字段。

出于对
$Dedity
的热爱,请阅读它的功能和不功能?你期望它做什么?他得到一个错误,所以提交不会返回false并阻止提交。。。告诉他解决办法<代码>文档.联系人表单.元素.名称大家好。。。回复的Thx将输入元素更改为-fullname,并使用了该javascript及其fine。再次向奥黛德和所有回复者致谢。