首页 > 解决方案 > 我的表单电子邮件输入有什么错误。提交时总是返回false

问题描述

我认为问题出在 jQuery 中。每当在电子邮件输入中输入某个值并尝试提交表单时,它会显示错误的天气,该值是否正确。请帮我。我的整个代码都已关闭,请仔细检查并彻底检查jQUERY SECTION,因为我认为问题不在 HTML 中,而是在 jQuery 中

回应将不胜感激。

 function isEmail(email) {

            var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;

                return regex.test(email);

            }
 $("#submit").click(function() {
 alert(isEmail($("#email").val()));
 });
 body{

 font-family: Lato;
 font-size: 200%
 }
 input{

 height: 40px;
 padding: 5px 5px 12px 5px;
 font-size: 25px;
 border-radius: 5px;
 border: 1px solid grey;
 width: 320px;
 }
 label{

 position: relative; 
 top: 5px;
 float: left;
 width: 275px;
 }
 #wrapper{

 width: 625px;
 margin: 0 auto;
 }
 .elements{
 margin-bottom: 10px;
 }
 #submit{
 width: 130px;
 margin-left: 275px;
 text-align: center;
 background-color: lightgrey;
 height: 55px;
 }
<!doctype html>
 <html>
 <head>
 <meta charset="UTF-8">
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
 <title>jQuery</title>
 </head>
 <body>

 <div id="wrapper">

 <div class="elements">
 <label for="Email">Email</label>
 <input type="text" name="Email" id="Email" placeholder="eg:yourname@gmail.com">
 </div>
 <div class="elements">
 <label for="Phone">Phone No</label>
 <input type="text" name="Phone" id="Phone No" placeholder="eg:1234567890">
 </div> 
 <div class="elements">
 <label for="Password">Password</label>
 <input type="password" name="Password" id="Password">
 </div> 
 <div class="elements">
 <label for="confirmPassword">ConfirmPassword</label>
 <input type="password" name="confirmPassword" id="confirmPassword">
 </div>
 <div class="elements">
 <input type="submit" id="submit" value="Sign Up">
 </div>
 </div>
 </body>
</html>

标签: javascriptjqueryhtml

解决方案


您与电子邮件的 ID 不匹配。尝试在 jQuery 中将 id 更改为#emailto #Email,它将起作用

$("#submit").click(function() {
console.log($("#Email").val());
 alert(isEmail($("#Email").val()));
 });
 
  function isEmail(email) {

           var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
           
    return re.test(String(email).toLowerCase());

            }
body{

 font-family: Lato;
 font-size: 200%
 }
 input{

 height: 40px;
 padding: 5px 5px 12px 5px;
 font-size: 25px;
 border-radius: 5px;
 border: 1px solid grey;
 width: 320px;
 }
 label{

 position: relative; 
 top: 5px;
 float: left;
 width: 275px;
 }
 #wrapper{

 width: 625px;
 margin: 0 auto;
 }
 .elements{
 margin-bottom: 10px;
 }
 #submit{
 width: 130px;
 margin-left: 275px;
 text-align: center;
 background-color: lightgrey;
 height: 55px;
 }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">

 <div class="elements">
 <label for="Email">Email</label>
 <input type="text" name="Email" id="Email" placeholder="eg:yourname@gmail.com">
 </div>
 
 <div class="elements">
 <label for="Phone">Phone No</label>
 <input type="text" name="Phone" id="Phone No" placeholder="eg:1234567890">
 </div> 
 
 <div class="elements">
 <label for="Password">Password</label>
 <input type="password" name="Password" id="Password">
 </div> 
 
 <div class="elements">
 <label for="confirmPassword">ConfirmPassword</label>
 <input type="password" name="confirmPassword" id="confirmPassword">
 </div>
 
 <div class="elements">
 <input type="submit" id="submit" value="Sign Up">
 </div>
 
 </div>


推荐阅读