2016-11-20 198 views
0

我创建了一个产品页面,在两个不同的地方人们可以输入他们的电子邮件。如果他们输入格式正确的电子邮件,我希望他们得到验证信息,如果他们输入了错误格式的电子邮件,我希望他们收到错误信息。如何使用Javascript进行验证和错误消息?

我的教授是有我们连接到他建在Ruby中,用来验证这对我们的Rails的数据库,并且已经成功连接到,但我们需要使用JavaScript来设置上出现错误和验证消息屏幕为用户。出于某种原因,我似乎无法让他们通过,我想知道是否有人对我在哪搞乱了什么有什么建议。

我的教授说,如果我们陷入困境,在这里寻求帮助很好,而且我真的陷入了困境。我一直在玩几个小时的选项,似乎无法解决任何问题。任何帮助将非常感激,谢谢!

这是它应该看,当你提交的格式不正确类似电子邮件:

enter image description here

这是当你提交的格式不正确的电子邮件会发生什么(没有出现在所有):

enter image description here

这是它应该看在您提交格式正确的电子邮件,如:

enter image description here

这是当你提交一个正确格式的电子邮件会发生什么(没有出现在所有):

enter image description here

这是在执行console.log会发生什么,当我点击在按钮上。

enter image description here

$(document).ready(function() { 
 
    
 
    $('form').submit(function(event) { 
 

 
     var formData = $(this).serialize(); 
 
     
 
     $.ajax({ 
 
      type   : 'POST', 
 
      url   : 'https://web2-product-page/herokuapp.com/subscribers', 
 
      data   : formData, 
 
      dataType  : 'json' 
 
     }).done(function(data) { 
 

 
       console.log(data); 
 
       $('.confirmation').fadeIn(); 
 
       $('.error-message').text(""); 
 
       $('input[name=email]').val(""); 
 
      }).fail(function(data) { 
 
       console.log(data); 
 
       var errorMessage = JSON.parse(data.responseText).email[0]; 
 
       $('.error-message').text(errorMessage); 
 
       $('.confirmation').hide(); 
 
      }); 
 
    
 
     event.preventDefault(); 
 
    });  
 
});
* { 
 
    box-sizing: border-box; 
 
} 
 
@font-face { 
 
    font-family: 'gilroysemibold'; 
 
    src: url('radomir_tinkov_-_gilroy-semibold-webfont.woff2') format('woff2'), 
 
     url('radomir_tinkov_-_gilroy-semibold-webfont.woff') format('woff'); 
 
    font-weight: normal; 
 
    font-style: normal; 
 
} 
 
body { 
 
    margin: 0px; 
 
} 
 
#calltoaction { 
 
    background-image: url("calltoactionbackground.jpg"); 
 
    background-size: 100%; 
 
    background-repeat: no-repeat; 
 
    display: inline-block; 
 
    height: 500px; 
 
    padding-bottom: 100px; 
 
    padding-left: 70px; 
 
    padding-right: 70px; 
 
    padding-top: 100px; 
 
    text-align: center; 
 
    width: 100%; 
 
} 
 
#calltoaction p { 
 
    margin: auto; 
 
    padding-top: 25px; 
 
    padding-bottom: 55px; 
 
    width: 500px; 
 
} 
 
.clearfix:before, 
 
.clearfix:after { 
 
    content: ""; 
 
    display: table; 
 
} 
 

 
.clearfix:after { 
 
    clear: both; 
 
} 
 

 
.clearfix { 
 
    zoom: 1; /* ie 6/7 */ 
 
} 
 
.confirmation { 
 
    display: none; 
 
} 
 
.error-message { 
 
    display: none; 
 
} 
 
#functions { 
 
    background-color: #FFFFFF; 
 
    display: block; 
 
    height: 1080px; 
 
    margin: auto; 
 
    padding-top: 100px; 
 
    width: 100%; 
 
} 
 
#functions p { 
 
    color: #62CE9C; 
 
} 
 
h1 { 
 
    color: #FFFFFF; 
 
    font-family: 'gilroysemibold'; 
 
    font-size: 36px; 
 
    font-weight: lighter; 
 
} 
 
h2 { 
 
    color: #62CE9C; 
 
    font-family: 'gilroysemibold'; 
 
    font-size: 30px; 
 
    font-weight: lighter; 
 
} 
 
h3 { 
 
    color: #00AF78; 
 
    font-family: Open Sans; 
 
    font-size: 18px; 
 
    line-height: 10px; 
 
} 
 
.hashtag { 
 
    color: \t #00AF78; 
 
    font-weight: bold; 
 
} 
 
#hero { 
 
    background-color: #62CE9C; 
 
    height: 650px; 
 
    margin: 0px; 
 
    padding-bottom: 100px; 
 
    padding-left: 120px; 
 
    padding-right: 120px; 
 
    padding-top: 100px; 
 
    width: 100%; 
 
} 
 
#herocontent { 
 
    margin: auto; 
 
    width: 900px; 
 
} 
 
#herotext { 
 
    float: left; 
 
    width: 600px; 
 
} 
 
#hero h1 { 
 
    width: 470px; 
 
} 
 
#hero img { 
 
    display: block; 
 
    float: right; 
 
} 
 
#hero p { 
 
    padding-top: 30px; 
 
    padding-bottom: 40px; 
 
    width: 500px; 
 
} 
 
input, select, textarea{ 
 
    color: #62CE9C; 
 
} 
 

 
textarea:focus, input:focus { 
 
    color: #62CE9C; 
 
} 
 
input::-webkit-input-placeholder { 
 
    color: #62CE9C !important; 
 
} 
 
    
 
input:-moz-placeholder { /* Firefox 18- */ 
 
    color: #62CE9C !important; 
 
} 
 
    
 
input::-moz-placeholder { /* Firefox 19+ */ 
 
    color: #62CE9C !important; 
 
} 
 
    
 
input:-ms-input-placeholder { 
 
    color: #62CE9C !important; 
 
} 
 
p { 
 
    color: #FFFFFF; 
 
    font-family: Open Sans; 
 
    font-size: 18px; 
 
    line-height: 26px; 
 
} 
 
#save { 
 
    align-items: center; 
 
    display: block; 
 
    margin: auto; 
 
    width: 800px; 
 
} 
 
#savefood { 
 
    float: right; 
 
    margin: auto; 
 
} 
 
#savefoodimage { 
 
    float: right; 
 
} 
 
#savefoodtext { 
 
    float: right; 
 
    height: 300px; 
 
    margin-right: 30px; 
 
    padding-top: 30px; 
 
    width: 290px; 
 
} 
 
#savemoney { 
 
    float: left; 
 
    margin: auto; 
 
} 
 
#savemoneyimage { 
 
    float: left; 
 
} 
 
#savemoneytext { 
 
    float: left; 
 
    height: 300px; 
 
    margin-left: 30px; 
 
    padding-top: 30px; 
 
    width: 330px; 
 
} 
 
#savetime { 
 
    float: left; 
 
    margin: auto; 
 
} 
 
#savetimeimage { 
 
    float: left; 
 
} 
 
#savetimetext { 
 
    float: left; 
 
    height: 300px; 
 
    margin-left: 30px; 
 
    padding-top: 30px; 
 
    width: 330px; 
 
} 
 
#searchbar { 
 
    background-color: #FFFFFF; 
 
    border: none; 
 
    border-radius: 8px; 
 
    font-size: 18px; 
 
    height: 40px; 
 
    padding-left: 15px; 
 
    width: 300px; 
 
} 
 
#searchbutton { 
 
    background-color: #28C787; 
 
    border: none; 
 
    border-radius: 8px; 
 
    color: #FFFFFF; 
 
    font-size: 18px; 
 
    height: 40px; 
 
    margin-left: 20px; 
 
    width: 180px; 
 
} 
 
#searchbutton:hover { 
 
    background-color: #00BE8B; 
 
} 
 
#tweetone { 
 
    background-color: #FFFFFF; 
 
    border-radius: 25px; 
 
    height: 208px; 
 
    margin: auto; 
 
    margin-top: 60px; 
 
    padding-bottom: 30px; 
 
    padding-left: 25px; 
 
    padding-right: 25px; 
 
    padding-top: 30px; 
 
    width: 650px; 
 
} 
 
#tweetone img { 
 
    float: left; 
 
    height: 150px; 
 
    padding-top: 10px; 
 
    width: 150px; 
 
} 
 
#tweetonetext { 
 
    float: left; 
 
    padding-left: 15px; 
 
    width: 450px; 
 
} 
 
#tweettwo { 
 
    background-color: #FFFFFF; 
 
    border-radius: 25px; 
 
    height: 208px; 
 
    margin: auto; 
 
    margin-top: 60px; 
 
    padding-bottom: 30px; 
 
    padding-left: 25px; 
 
    padding-right: 25px; 
 
    padding-top: 30px; 
 
    width: 650px; 
 
} 
 
#tweettwo img { 
 
    float: left; 
 
    padding-top: 10px; 
 
} 
 
#tweettwotext { 
 
    float: left; 
 
    padding-left: 15px; 
 
    width: 400px; 
 
} 
 
#tweetthree { 
 
    background-color: #FFFFFF; 
 
    border-radius: 25px; 
 
    height: 208px; 
 
    margin: auto; 
 
    margin-top: 60px; 
 
    padding-bottom: 30px; 
 
    padding-left: 25px; 
 
    padding-right: 25px; 
 
    padding-top: 30px; 
 
    width: 650px; 
 
} 
 
#tweetthree img { 
 
    float: left; 
 
    padding-top: 10px; 
 
} 
 
#tweetthreetext { 
 
    float: left; 
 
    padding-left: 15px; 
 
    width: 450px; 
 
} 
 
ul { 
 
    list-style-type: none; 
 
} 
 
#vocational { 
 
    background-color: #62CE9C; 
 
    display: inline-block; 
 
    height: 1120px; 
 
    padding-bottom: 100px; 
 
    padding-left: 70px; 
 
    padding-right: 70px; 
 
    padding-top: 100px; 
 
    width: 100%; 
 
} 
 
#vocational h1 { 
 
    margin: auto; 
 
    text-align: center; 
 
    margin-bottom: 0px; 
 
    width: 500px; 
 
} 
 
#vocational p { 
 
    color: #62CE9C; 
 
    display: inline-block; 
 
}
<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <link href="css/main.css" rel="stylesheet"/> 
 
    <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet"> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
    <script src="js/main.js"></script> 
 
    </head> 
 
    <body> 
 
    <div class="container clearfix" id="hero"> 
 
     <div id="herocontent"> 
 
     <div id="herotext"> 
 
      <h1>Make the Most of your Food With Pantree</h1> 
 
      <p>Pantree for iOS lets you search for recipes based on the ingredients you already have in your home.</p> 
 
      <form id="subscribe" method="post" action="https://web-2-product-page.herokuapp.com/subscribers"> 
 
      <input id="searchbar" type="text" name="email" placeholder="[email protected]"> 
 
      <input id="searchbutton" type="submit" value="Get Early Access"> 
 
      <p class="confirmation">Thank you!</p> 
 
      <p class="error-message">Incorrect E-mail Address.</p> 
 
      </form> 
 
     </div> 
 
     <img src="images/phone.png"/> 
 
     </div> 
 
    </div> 
 
    <div id="functions"> 
 
     <div id=save> 
 
     <div id="savemoney"> 
 
      <img id="savemoneyimage" src="images/savemoney.png"/> 
 
      <div id="savemoneytext"> 
 
      <h2>Save Money</h2> 
 
      <p>Pantree finds you recipes containing ingredients you already have in your home, saving you from unecessary trips to the grocery store.</p> 
 
      </div> 
 
     </div> 
 
     <div id="savefood"> 
 
      <img id="savefoodimage" src="images/savefood.png"/> 
 
      <div id="savefoodtext"> 
 
      <h2>Save Food</h2> 
 
      <p>Pantree keeps track of expiration dates, alerting you when food will go stale so you can use it before it goes bad.</p> 
 
      </div> 
 
     </div> 
 
     <div id="savetime"> 
 
      <img id="savetimeimage" src="images/savetime.png"/> 
 
      <div id="savetimetext"> 
 
      <h2>Save Time</h2> 
 
      <p>Pantree's built-in kitchen organizing system helps you monitor all of the food in your home, so figuring out what food you have is quick & easy.</p> 
 
      </div> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    <div id="vocational"> 
 
     <h1>These Folks Could Use Pantree Every Day</h1> 
 
     <ul> 
 
     <li> 
 
      <div id="tweetone"> 
 
      <img src="images/tweetone.png"/> 
 
      <div id="tweetonetext"> 
 
       <h3>Kat</h3> 
 
       <h3>@devicat</h3> 
 
       <p>I have no idea what to make for dinner. I am so bad at this game. <span class="hashtag">#adulting</span></p> 
 
      </div>   
 
      </div> 
 
     </li> 
 
     <li> 
 
      <div id="tweettwo"> 
 
      <img src="images/tweettwo.png"/> 
 
      <div id="tweettwotext"> 
 
       <h3>Jack Falahee</h3> 
 
       <h3>@RestingPlatypus</h3> 
 
       <p>Dear Mom, How do I organize my kitchen? Love, me</p> 
 
      </div> 
 
      </div> 
 
     </li> 
 
     <li> 
 
      <div id="tweetthree"> 
 
      <img src="images/tweetthree.png"/> 
 
      <div id="tweetthreetext"> 
 
       <h3>mason ryan</h3> 
 
       <h3>@MasonTheManiac</h3> 
 
       <p>Something in my fridge smells really bad.... <span class="hashtag">#cantfindit</span></p> 
 
      </div> 
 
      </div> 
 
     </li> 
 
     </ul> 
 
    </div> 
 
    <div id="calltoaction"> 
 
     <h1>Manage your Kitchen, Effortlessly</h1> 
 
     <p>Pantree makes it easy to find recipes, keep track of food, and organize your kitchen.</p> 
 
     <form id="subscribe" method="post" action="https://web-2-product-page.herokuapp.com/subscribers"> 
 
     <input id="searchbar" type="text" name="email" placeholder="[email protected]"> 
 
     <input id="searchbutton" type="submit" value="Get Early Access"> 
 
     <p class="confirmation">Thank you!</p> 
 
     <p class="error-message">Incorrect E-mail Address.</p> 
 
     </form> 
 
    </div> 
 
    </body> 
 
</html>

+0

如果您准确地发布了您尝试过的内容,以及您获得的结果和期望的结果会更好;如果你的问题是关于JS功能的话,CSS通常不需要发布。另外,来自AJAX调用的JSON响应对于格式正确的电子邮件和格式错误的电子邮件来说是什么样的?请记住,AJAX调用中的'.fail()'回调是指当对指定的URL调用失败时(即返回HTTP错误代码) - 而您可能需要检查JSON以获得对电子邮件的实际评估用户提交。 – Stevangelista

+0

实际上,CSS可以在这里帮助,因为问题是关于页面的显示方式。 –

+0

你的jQuery似乎没问题。你知道你的Ajax调用是否成功吗? (如果它被错误配置,你将永远得到你的“失败”条件)http://stackoverflow.com/questions/21897398/how-do-i-debug-jquery-ajax-calls –

回答

1

两点:

1)你已经在你的代码(https://web2-product-page/herokuapp.com/subscribers)返回404指定的API端点没有找到,所以你永远不会从中得到回应。
我相信您误解了 .done()和.fail()方法的用途 - 这些不会响应通过或失败电子邮件验证而被调用,而是响应服务器的通过/失败请求。在你的代码中,因为服务器返回404,所以总会调用fail()。由于响应失败,因此不会使用data.responseText。
我会用更多的东西一样以下(假设当服务器存在,它返回data.validEmail代表电子邮件的有效性(实际的服务器可能使用不同的东西)。

$.ajax({ 
     type   : 'POST', 
     url   : 'https://web2-product-page/herokuapp.com/subscribers', 
     data   : formData, 
     dataType  : 'json' 
    }).done(function(data) { 

      if (data.validEmail) { 
       $('.confirmation').fadeIn(); 
       $('.error-message').text(""); 
       $('input[name=email]').val(""); 
      } else { 
       $('.error-message').text("Not a valid email address"); 
      } 


     }).fail(function() { 
      $('.error-message').text("The server did not respond.");     
     }); 

2)虽然这是一个有效的学习练习通过AJAX与远程服务器通信,实际上,验证电子邮件的最简单方法是在输入中使用HTML5类型属性。

<input type="email" id="searchbar" name="email" placeholder="[email protected]"> 

现在浏览器会做的辛勤工作为你提供一个验证信息并阻止表单提交,除非它是有效的。

编辑: 如果您发布一个有效的电子邮件到您的API,然后返回与已保存电子邮件的细节JSON对象,而“201创建”状态,您可以测试如下:

$.ajax({ 
     type   : 'POST', 
     url   : 'https://web2-product-page/herokuapp.com/subscribers', 
     data   : formData, 
     dataType  : 'json' 
    }).done(function(data) { 

      if (data.status === 201) { 
       $('.confirmation').fadeIn(); 
       $('.error-message').text(""); 
       $('input[name=email]').val(""); 
      } else { 
       $('.error-message').text(data.email); 
      } 


     }).fail(function() { 
      $('.error-message').text("The server did not respond.");     
     }); 
+0

嗨,谢谢你,我会联系我的教授关于API问题并实施你的建议。谢谢大家的帮助,非常感谢。一旦我确定API问题已解决,并且我有机会正确使用它来验证此答案,我会将其标记为答案,谢谢。 –

+0

没问题。我只是看了一下这个API的URL,发现你有一个斜杠,你应该有一个点!正确的URL是https://web2-product-page.herokuapp.com/subscribers –

+0

我已经多了一点与使用邮差的API玩并编辑我的答案。 –