电子邮件验证:JavaScript
我正在尝试创建一个表单来验证电子邮件地址。“提交”按钮不起作用,或者是电子邮件验证脚本导致了问题。我不熟悉编码,需要一些帮助。提前谢谢你电子邮件验证:JavaScript,javascript,jquery,html,email-validation,Javascript,Jquery,Html,Email Validation,我正在尝试创建一个表单来验证电子邮件地址。“提交”按钮不起作用,或者是电子邮件验证脚本导致了问题。我不熟悉编码,需要一些帮助。提前谢谢你 <!doctype html> <html> <head> <title>JQuery Lesson</title> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> &
<!doctype html>
<html>
<head>
<title>JQuery Lesson</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js">
</script>
<style>
#wrapper{
margin: 0 auto;
width: 500px;
font-family: helvetica;
font-size: 1.2em;
}
#error {
color: red;
margin: 20px;
}
#email {
height: 30px;
width: 300px;
border-radius: 20px;
padding-top: 10px;
margin-bottom: 15px;
font-size: 1.2em;
float: none;
margin: 0 auto;
}
#phone {
height: 30px;
width: 300px;
border-radius: 20px;
padding-top: 10px;
margin-bottom: 15px;
font-size: 1.2em;
float: none;
margin: 0 auto;
}
#password {
height: 30px;
width: 300px;
border-radius: 20px;
padding-top: 10px;
margin-bottom: 15px;
font-size: 1.3em;
float: none;
margin: 0 auto;
}
#confirmPassword {
height: 30px;
width: 300px;
border-radius: 20px;
padding-top: 10px;
margin-bottom: 15px;
font-size: 1.3em;
float: none;
margin: 0 auto;
}
.spacer {
font-size:0;
height: 20px;
line-height: 0;
}
label {
width: 90px;
float:left;
padding-top: 10px;
}
#submitButton {
height: 30px;
width: 90px;
font-size: 1.1em;
float: none;
margin: 0 auto;
}
</style>
</head>
<body>
<div id= "wrapper"; >
<form id = "validationForm">
<div id = "error"></div>
<label for = "email"> Email </label>
<input id = "email" />
<div class="spacer"></div>
<label for = "phone"> Phone </label>
<input id = "phone" />
<div class="spacer"></div>
<label for = "password"> Password </label>
<input id = "password", type = "password" />
<div class="spacer"></div>
<label for = "confirm password"> Confirm Password </label>
<input id = "confirmPassword", type = "password" />
<div class="spacer"></div>
<button id = "submitButton" type="submit" value="submit"> Submit
</button>
</form>
</div>
<script>
function validateEmail(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(email);
};
if (!validEmail($("#email").val()))) {
$("#error").html("Please enter a valid email address.");
}
;
</script>
</body>
</html>
JQuery课程
#包装纸{
保证金:0自动;
宽度:500px;
字体系列:helvetica;
字体大小:1.2米;
}
#错误{
颜色:红色;
利润率:20px;
}
#电子邮件{
高度:30px;
宽度:300px;
边界半径:20px;
填充顶部:10px;
边缘底部:15px;
字体大小:1.2米;
浮动:无;
保证金:0自动;
}
#电话{
高度:30px;
宽度:300px;
边界半径:20px;
填充顶部:10px;
边缘底部:15px;
字体大小:1.2米;
浮动:无;
保证金:0自动;
}
#密码{
高度:30px;
宽度:300px;
边界半径:20px;
填充顶部:10px;
边缘底部:15px;
字体大小:1.3em;
浮动:无;
保证金:0自动;
}
#确认密码{
高度:30px;
宽度:300px;
边界半径:20px;
填充顶部:10px;
边缘底部:15px;
字体大小:1.3em;
浮动:无;
保证金:0自动;
}
.垫片{
字号:0;
高度:20px;
线高:0;
}
标签{
宽度:90px;
浮动:左;
填充顶部:10px;
}
#提交按钮{
高度:30px;
宽度:90px;
字体大小:1.1米;
浮动:无;
保证金:0自动;
}
电子邮件
电话
密码
确认密码
提交
功能验证电子邮件(电子邮件){
变量re=/^(([^()\[\]\\,;:\s@“]+(\.[^()\[\]\\,;:\s@“]+)*)|
(“+”)@(\[[0-9]{1,3}.[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,}))$/;
返回重新测试(电子邮件);
};
如果(!validEmail($(“#email”).val()){
$(“#错误”).html(“请输入有效的电子邮件地址”);
}
;
首先,validEmail函数中有一个错误。请尝试以下方法:
功能有效邮件(电子邮件){
变量re=/^([^()\[\]\\,;:\s@“]+(\.[^()\[\]\,;:\s@“]+)*)(“+”)(\[[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[1,3}.[0-9]{1,3}.]124;([a-zA Z-0-9]++\];
返回重新测试(电子邮件);
};
您是否只尝试了
?使用
接受电子邮件地址,您就不必编写任何JavaScript。(还有一个好处是不会像这个正则表达式那样拒绝有效的电子邮件地址。)那么您在哪里调用了这个validateEmail()函数呢?您需要提供submitform方法并在那里调用此验证。