在Javascript xhtml 1.0 strict中验证表单数据,而不考虑此实例的服务器端脚本

时间:2013-06-26 14:38:33

标签: javascript function validation email-validation xhtml-1.0-strict

还有其他关于使用javascript验证电子邮件地址的问题。还有关于验证表格的问题。但是我无法让我的代码工作,也无法找到解决这一特定问题的问题。

修改

我完全理解在实时网站中,服务器端验证至关重要。我也理解发送电子邮件确认的价值。 (我实际上有一个具有所有这些功能的网站)。我知道如何在php中编写垃圾邮件检查。

在这种情况下,我被要求验证电子邮件输入字段。我必须符合xhtml 1.0 strict,所以不能使用“email”类型,我不允许使用服务器端脚本进行此分配。我无法组织电子邮件确认,必须通过javascript完全检查。

我希望这能澄清我的问题

我正在尝试验证两个表单的表单。

检查所有字段是否都有数据。 查看是否输入了有效的电子邮件地址。

我可以验证数据的表单字段,但尝试合并电子邮件检查对我来说是一个麻烦。

它之前发出警报,但错误地说,现在它根本没有被调用(或者至少它是这样的行为)。

一旦我开始工作,我就需要专注于检查电子邮件地址是否匹配。然而,除了这个问题之外,这是一个问题。

我只专注于在javascript中验证这一点。我不关心这个特定实例中的服务器端(此问题之外的另一个问题)。感谢。

function Validate()
        {
            var inputs = [document.getElementById('fname'),_
 document.getElementById('lname'), document.getElementById('email1'),_
 document.getElementById('email2')];

            for(var i = 0; i<inputs.length; i++)
            {
                if(inputs[i].value == '')
                {
                   alert('Please complete all required fields.');
                    return false;
                    }

                    else if ((id =='email1' || 'email2') &&_
(inputs[i].value!= /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ )){
                        alert('Please enter a valid email address.');
                        return false;
                    }
            }
         }


<form onsubmit="return Validate()"  action="" method="post"  id="contactForm" >
 <input  type="text"  name="fname" id="fname"  />

 <input  type="text"  name="lname" id="lname" />

 <input   type="text"  name="email1"  id="email1" />

 <input   type="text"  name="email2"  id="email2"/>

 <input type="submit" value="submit" name="submit"  />
</form>

附注 - 格式化包装的文本,是否正常(为了发布问题,添加和下划线并创建新行以便于阅读?在实际文本中我没有这个!请告知是否有更简单的方法来格式化我的帖子代码。再次感谢。

修改2

当我发表评论时,它会起作用:

/*else if ((id =='email1' || id=='email2') && (inputs[i].value!= /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ )){
    alert('Please enter a valid email address.');
        return false;
}*/

所以这有助于解决问题。

4 个答案:

答案 0 :(得分:3)

 var a=document.getElementById('fname');
 var b=document.getElementById('lname');
 var c=document.getElementById('email1');
 var d=document.getElementById('email12')

if(a==""||b==""||c==""||d=="")
 {
 alert('Please complete all required fields.');
 return false;
 }

答案 1 :(得分:3)

我已经在那里看到语法错误:

else if ((id =='email1' || 'email2') 

应该是

else if ((id =='email1' || id=='email2') 
从我看到的地方开始。

另请注意,在任何字段中输入空格也会通过测试:在测试空字段时,应修剪字段值。

最后,关于验证电子邮件,这不是你使用正则表达式的方式。请阅读this post以获取有关如何在javascript + regex中验证电子邮件的演示。

答案 2 :(得分:3)

验证电子邮件地址最好的办法是向该地址发送电子邮件。正则表达式不适用于验证电子邮件地址。您可能能够验证正常的,例如john.doe@email.com,但如果您使用正则表达式,还有其他有效的电子邮件地址,您将拒绝

结帐Regexp recognition of email address hard? AND:Using a regular expression to validate an email address

答案 3 :(得分:1)

我按如下方式解决了我的问题。我也在这里检查电子邮件是否匹配。

// JavaScript Document
//contact form function

 function ValidateInputs(){
 /*check that fields have data*/

 // create array containing textbox elements
 var inputs = [document.getElementById("fname"),_
document.getElementById("lname"), document.getElementById("message"),_
document.getElementById("email1"), document.getElementById("email2")];

    for(var i = 0; i<inputs.length; i++){
 // loop through each element to see if value is empty
        if(inputs[i].value == ""){

            alert("Please complete all fields.");
            return false;
        }
        else if ((email1.value!="") && (ValidateEmail(email1)==false)){

            return false;
        }
        else if ((email2.value!="") && (EmailCheck(email2)==false)){

            return false;
        }
    }
}   


function ValidateEmail(email1){
/*check for valid email format*/
    var reg =/^.+@.+$/;

    if (reg.test(email1.value)==false){

        alert("Please enter a valid email address.");
        return false;

    }
}

function EmailCheck(email2){

    var email1 = document.getElementById("email1");
    var email2 = document.getElementById("email2");

    if ((email2.value)!=(email1.value)){

        alert("Emails addresses do not match.");
        return false;
    }
}

<form onsubmit="return ValidateInputs();" method="post"  id="contactForm">

    <input  type="text"  name="fname" id="fname"  />
    <input  type="text"  name="lname" id="lname" />
    <input   type="text" onblur="return ValidateEmail(this);"  name="email1"  id="email1" />
    <input   type="text" onblur="return EmailCheck(this);" name="email2"  id="email2"/>
    <input type="submit" value="submit" name="submit"  />
</form>
相关问题