javascript - 我的表单电子邮件输入有什么错误。提交时总是返回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>
解决方案
您与电子邮件的 ID 不匹配。尝试在 jQuery 中将 id 更改为#email
to #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>
推荐阅读
- c# - 如何在鼠标按下时同时旋转多个对象?
- windows - 是否有 PowerShell Get-Content 函数可以根据第一个字符提取行?
- javascript - 在 div 选择和使用事件传播的父选择之后将 div 子从一个父级移动到另一个父级
- python - 将文本文件数据复制到另一个没有第一行的文件
- r - 如果日期结束,如何将所有编号行重置为全部 1
- javascript - 使用 Elementor 中的 HTML 小部件将 Javascript 添加到页面时出现 403 服务器错误
- swift - 在 Swift 中调整从 API 下载的 pdf 文档的大小
- macos - OS X 还是自制软件?我如何检查
- asp.net-mvc - 500 - 没有详细信息的内部服务器错误
- java - Spring gradle build jar 找不到带有 NoClassDefFoundError 的主类