如何在提交表单之前验证电子邮件字段是否为空
How to verify that email field is not blank before submitting form
更新问题:
我在所有字段中添加了 "required",但如果
表单仍会提交
first@second 用作电子邮件。即使缺少 .com(或其他),它也会提交。
我怎样才能将此验证也纳入其中?
谢谢!
原题:
我的站点有一个注册表单,其中只有一个电子邮件字段。目前有验证器来验证输入的电子邮件的语法是否正确,但如果该字段留空,表单 将 提交。我需要在 提交之前验证该字段是否为空 。如果它是空白的,应该会出现一些消息 - 类似于出现的消息,例如没有包含 @ 符号。
我的演示页面是 here.
表格html:
<div class='form animated flipInX'>
<h2>Sign Up</h2>
<form action="http://mydomain.us10.list-manage.com/subscribe/post" method="POST">
<input type="hidden" name="u" value="a324dfsf32erwdafdaf3dfsdsdf">
<input type="hidden" name="id" value="32df32rff2">
<input type="email" name="MERGE0" id="MERGE0" placeholder="Your Email Address" class="boxfield">
<button class='animated infinite pulse'>Let's Go!</button>
</form>
</div>
有什么建议吗?谢谢
您可以在输入标签中添加 required
属性。
<input type="email" name="MERGE0" id="MERGE0" placeholder="Your Email Address" class="boxfield" required="true">
幸好就是这么简单
执行此操作的最佳方法是在输入标签中添加 required
属性
在您的输入标签中使用 required
所需的属性是布尔属性。
如果存在,它指定必须在提交表单之前填写输入字段。
所以你的输入应该是
<input type="email" name="MERGE0" id="MERGE0" placeholder="Your Email Address" class="boxfield" required>
在这里寻找 more information
更新:
type="email"
是html5的共同属性。如果您需要验证,您应该在输入元素中使用 patten
你应该使用pattern="[a-z0-9!#$%&'*+/=?^_
{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b
这里是应该用的一个
<input type="email" required pattern="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b">
根据您更新的问题
,这是 jsfiddle
请检查此解决方案,我在其中使用 jquery 检查它是空白还是填充
<div class='form animated flipInX'>
<h2>Sign Up</h2>
<form action="http://mydomain.us10.list-manage.com/subscribe/post" method="POST">
<input type="hidden" name="u" value="a324dfsf32erwdafdaf3dfsdsdf">
<input type="hidden" name="id" value="32df32rff2">
<input type="email" name="MERGE0" id="MERGE0" placeholder="Your Email Address" class="boxfield">
<button class='animated infinite pulse' type="submit" onClick="checkval();">Let's Go!</button>
</form>
</div>
<script>
function checkval()
{
var email_val=$("#MERGE0").val();
if(email_val.length>2)
{
}
else
{
alert("email is required.");/*there you change show the value which you want to show as a error message.*/
event.preventDefault();
/* this help to stop submit form*/
}
}
</script>
希望这对您有所帮助
我不知道你的问题是否已经得到解答,但试试这个:
//The Input
<input type='email' id='email' placeholder='email' onchange='emailCheck()'>
//The Button
<button id='button' style='visibility:hidden'>Submit</button>
//Javascript
<script>
var input = document.getElementById('email').value;
if(input.indexOf("@") > -1){
if(input.indexOf(".com") >= input.length - 4){
document.getElementById('button').style.visibility = 'visible';
}else{
alert('Custom Invalid Email Alert');
}
}else{
alert('Custom Invalid Email Alert');
}
</script>
必需的是最好的选择,或者你可以使用 java 脚本
像这样
<script type="text/javascript">
function validateForm()
{
var a=document.forms["Form"]["answer_a"].value;
var b=document.forms["Form"]["answer_b"].value;
var c=document.forms["Form"]["answer_c"].value;
var d=document.forms["Form"]["answer_d"].value;
if (a==null || a=="",b==null || b=="",c==null || c=="",d==null || d=="")
{
alert("Please Fill All Required Field");
return false;
}
}
</script>
<form method="post" name="Form" onsubmit="return validateForm()" action="">
<textarea cols="30" rows="2" name="answer_a" id="a"></textarea>
<textarea cols="30" rows="2" name="answer_b" id="b"></textarea>
<textarea cols="30" rows="2" name="answer_c" id="c"></textarea>
<textarea cols="30" rows="2" name="answer_d" id="d"></textarea>
</form>
更新问题:
我在所有字段中添加了 "required",但如果
表单仍会提交first@second 用作电子邮件。即使缺少 .com(或其他),它也会提交。
我怎样才能将此验证也纳入其中?
谢谢!
原题:
我的站点有一个注册表单,其中只有一个电子邮件字段。目前有验证器来验证输入的电子邮件的语法是否正确,但如果该字段留空,表单 将 提交。我需要在 提交之前验证该字段是否为空 。如果它是空白的,应该会出现一些消息 - 类似于出现的消息,例如没有包含 @ 符号。
我的演示页面是 here.
表格html:
<div class='form animated flipInX'>
<h2>Sign Up</h2>
<form action="http://mydomain.us10.list-manage.com/subscribe/post" method="POST">
<input type="hidden" name="u" value="a324dfsf32erwdafdaf3dfsdsdf">
<input type="hidden" name="id" value="32df32rff2">
<input type="email" name="MERGE0" id="MERGE0" placeholder="Your Email Address" class="boxfield">
<button class='animated infinite pulse'>Let's Go!</button>
</form>
</div>
有什么建议吗?谢谢
您可以在输入标签中添加 required
属性。
<input type="email" name="MERGE0" id="MERGE0" placeholder="Your Email Address" class="boxfield" required="true">
幸好就是这么简单
执行此操作的最佳方法是在输入标签中添加 required
属性
在您的输入标签中使用 required
所需的属性是布尔属性。
如果存在,它指定必须在提交表单之前填写输入字段。
所以你的输入应该是
<input type="email" name="MERGE0" id="MERGE0" placeholder="Your Email Address" class="boxfield" required>
在这里寻找 more information
更新:
type="email"
是html5的共同属性。如果您需要验证,您应该在输入元素中使用 patten
你应该使用pattern="[a-z0-9!#$%&'*+/=?^_
{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b
这里是应该用的一个
<input type="email" required pattern="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b">
根据您更新的问题
,这是 jsfiddle请检查此解决方案,我在其中使用 jquery 检查它是空白还是填充
<div class='form animated flipInX'>
<h2>Sign Up</h2>
<form action="http://mydomain.us10.list-manage.com/subscribe/post" method="POST">
<input type="hidden" name="u" value="a324dfsf32erwdafdaf3dfsdsdf">
<input type="hidden" name="id" value="32df32rff2">
<input type="email" name="MERGE0" id="MERGE0" placeholder="Your Email Address" class="boxfield">
<button class='animated infinite pulse' type="submit" onClick="checkval();">Let's Go!</button>
</form>
</div>
<script>
function checkval()
{
var email_val=$("#MERGE0").val();
if(email_val.length>2)
{
}
else
{
alert("email is required.");/*there you change show the value which you want to show as a error message.*/
event.preventDefault();
/* this help to stop submit form*/
}
}
</script>
希望这对您有所帮助
我不知道你的问题是否已经得到解答,但试试这个:
//The Input
<input type='email' id='email' placeholder='email' onchange='emailCheck()'>
//The Button
<button id='button' style='visibility:hidden'>Submit</button>
//Javascript
<script>
var input = document.getElementById('email').value;
if(input.indexOf("@") > -1){
if(input.indexOf(".com") >= input.length - 4){
document.getElementById('button').style.visibility = 'visible';
}else{
alert('Custom Invalid Email Alert');
}
}else{
alert('Custom Invalid Email Alert');
}
</script>
必需的是最好的选择,或者你可以使用 java 脚本
像这样
<script type="text/javascript">
function validateForm()
{
var a=document.forms["Form"]["answer_a"].value;
var b=document.forms["Form"]["answer_b"].value;
var c=document.forms["Form"]["answer_c"].value;
var d=document.forms["Form"]["answer_d"].value;
if (a==null || a=="",b==null || b=="",c==null || c=="",d==null || d=="")
{
alert("Please Fill All Required Field");
return false;
}
}
</script>
<form method="post" name="Form" onsubmit="return validateForm()" action="">
<textarea cols="30" rows="2" name="answer_a" id="a"></textarea>
<textarea cols="30" rows="2" name="answer_b" id="b"></textarea>
<textarea cols="30" rows="2" name="answer_c" id="c"></textarea>
<textarea cols="30" rows="2" name="answer_d" id="d"></textarea>
</form>