简体   繁体   English

JavaScript订阅表单验证错误

[英]Javascript Subscribe Form Validation Errors

I'm building a basic subscribe form which uses Javascript for validation, but it's not working correctly. 我正在构建使用Javascript进行验证的基本订阅表单,但无法正常工作。

My Form code is: 我的表单代码是:

<form id="pageSubscribeForm">
    <div class="pageSubscribeCell">
        <input id="pageSubscribeName" type="text" class="pageSubscribeInput" value="Tell Us Your Name" />
    </div>
    <div class="pageSubscribeCell">
        <input id="pageSubscribeEmail" type="text" class="pageSubscribeInput" value="Add Your Email Address" />
    </div>          
    <div class="pageSubscribeCell hidden">
        <input id="pageSubscribeSpam" type="text" class="pageSubscribeInput" value="" />
    </div>
    <div class="pageSubscribeCell">
        <div class="pageSubscribeButton">
            <div id="pageSubscribeSubmit" class="pageSubscribeAction"></div>
        </div>
    </div>
</form>

And the validation code is: 验证代码为:

$("#pageSubscribeSubmit").click(function() {
var name = $("#pageSubscribeName").val();
var nameok = 0;
var email = $("#pageSubscribeEmail").val();
var emailok = 0;
var spam = $("#pageSubscribeSpam").val();
var spamok = 0; 
var dataString = 'name='+ name + '&email=' + email + '&spam=' + spam;

if(name!='Tell Us Your Name'){
    nameok = 1
}else{
    $("#pageSubscribeName").removeClass("subscribeSuccess").addClass("subscribeError");
}

if(email!='Add Your Your Email Address'){
    emailok = 1
}else{
    $("#pageSubscribeEmail").removeClass("subscribeSuccess").addClass("subscribeError");
}

if(spam ==''){
    spamok = 1
}else{
    $("#pageSubscribeSpam").removeClass("subscribeSuccess").addClass("subscribeError");
}

if(nameok == 1 && emailok == 1 && spamok == 1){
    $('#pageSubscribeSubmit').hide();
}

When I click on the submit link I get the error class added to my Name cell but the email cell stays the same and the spam cell stays hidden. 当我单击“ submit链接时,我将错误类别添加到“名称”单元格中,但电子邮件单元格保持不变,而垃圾邮件单元格保持隐藏状态。 Any ideas why or how to fix? 任何想法为什么或如何解决?

To fix YOUR code, remove the second "Your" in the test 要修复您的代码,请删除测试中的第二个“您的”

if(email!='Add Your Your Email Address'){ if(email!='添加您电子邮件地址'){

To avoid such problems I strongly suggest something like this which works 为避免此类问题,我强烈建议使用类似的方法

 $(function() { $("#pageSubscribeSubmit").on("click", function() { var name = $.trim($("#pageSubscribeName").val()); var email = $.trim($("#pageSubscribeEmail").val()); var spam = $("#pageSubscribeSpam").val(); var dataString = 'name=' + name + '&email=' + email + '&spam=' + spam; $("#pageSubscribeName").toggleClass("pageSubscribeError", name == ""); $("#pageSubscribeEmail").toggleClass("pageSubscribeError", email == ""); var spamok = spam == $("#pageSubscribeSpam").get(0).defaultValue; // nothing changed $("#pageSubscribeSpam").toggle(!spamok); if (spamok && $(".pageSubscribeError").length == 0) { // no errors $('#pageSubscribeSubmit').hide(); $.ajax({ type: "POST", url: "php/subscribe.php", data: dataString, success: function() { $('#pageSubscribeSubmit').slideUp(); } }); } }); }); 
 .pageSubscribeError { border:1px solid red } #pageSubscribeSpam { display:none } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <form id="pageSubscribeForm"> <div class="pageSubscribeCell"> <input id="pageSubscribeName" type="text" class="pageSubscribeInput" value="" placeholder="Tell Us Your Name" /> </div> <div class="pageSubscribeCell"> <input id="pageSubscribeEmail" type="text" class="pageSubscribeInput" value="" placeholder="Add Your E-Mail Address" /> </div> <div class="pageSubscribeCell hidden"> <input id="pageSubscribeSpam" type="text" class="pageSubscribeInput" value="khujh" /> </div> <div class="pageSubscribeCell"> <div class="pageSubscribeButton"> <div id="pageSubscribeSubmit" class="pageSubscribeAction">Submit</div> </div> </div> </form> 

Your email field doesn't get the error class because your test is checking the wrong text (2x 'Your'): 您的电子邮件字段未获得错误类别,因为您的测试正在检查错误的文本(2x“您的”):

if(email!='Add Your Your Email Address'){

You also asked why "... the spam cell stays hidden ...". 您还问为什么“ ...垃圾邮件单元保持隐藏...”。 There is nothing in the code you posted which would remove the .hidden class from the div around #pageSubscribeSpam. 您发布的代码中没有任何内容可将div中的.hidden类从#pageSubscribeSpam周围删除。 Assuming your .hidden class does what it suggests, then the #pageSubscribeSpam input is hidden on page load. 假设您的.hidden类执行了建议的操作,则#pageSubscribeSpam输入在页面加载时被隐藏。 There is no way for a user to enter a value, so when the button is clicked, its value is still ''. 用户无法输入值,因此单击按钮时,其值仍为“”。 So spamok = 1 , always - it will never get the .subscribeError class added. 因此spamok = 1始终-永远不会添加.subscribeError类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM