我有一个横跨两页的表单。我正在尝试验证电子邮件地址,但每当我输入一封糟糕的电子邮件时,我都会在控制台中收到一个错误,说无效的表单控件无法聚焦
测试代码段的最简单方法:
- 将名字和姓氏留空
- 输入单个字母作为电子邮件地址
- 单击下一个
- 点击提交
- 检查控制台是否有错误消息
此stackoverflow答案不可聚焦不回答问题。电子邮件输入没有"REQUIRED",除提交外,所有按钮的类型都为"BUTTON",但有一个与之相关的点击功能。
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://www.sustainablewestonma.org/swag/public/css/getInvolved001.css?test1=apple">
<div id='container'>
<div id='section-container'>
<div id='section1' class='sections'>
<div id='section1b'>
<span class='majCap'>Get Involved</span>
<span class='minCap'>Enter your details below</span>
<form action='getInvolvedCtrl.php' method='POST' id='myForm' onsubmit="return mySubmit(event)">
<div class='columns'>
<input class='detailsC' type="text" name="fname" placeholder="First Name" id='fname'>
<input class='detailsC' type="text" name="lname" placeholder="Last Name" id='lname'>
<input class='detailsC' type="email" name="email" placeholder="Email..." id='email'>
</div>
<div class='rowsDet'>
<span class='detailsR'><input type="checkbox" name="wres">Weston Resident</span>
<span class='detailsR'><input type="checkbox" name="news">Quarterly Newsletter</span>
</div>
<div class='spacer'></div>
<span class="minCap" >Areas of Interests</span>
<div class='rowsInt'>
<div class="columns">
<span><input type="checkbox" name="energy-int">Energy</span>
<span><input type="checkbox" name="recycling-int">Recycling</span>
<span><input type="checkbox" name="composting-int">Composting</span>
<span><input type="checkbox" name="transport-int">Transportation</span>
</div>
<div class="columns">
<span><input type="checkbox" name="climate-int">Climate</span>
<span><input type="checkbox" name="trees-int">Trees</span>
<span><input type="checkbox" name="pollinators-int">Pollinators</span>
<span><input type="checkbox" name="water-int">Water</span>
<span><input type="checkbox" name="other-int">Other</span>
</div>
</div>
<button id='next' type='button' onclick="nextPage()">next</button>
</div>
<div id='section2' class='sections'>
<span class='minCap' >Current Campaign Information:</span>
<div class='rowsCam'>
<div class="columns">
<span><input type="checkbox" name="cleanup-cpg">Town Cleanup</span>
<span><input type="checkbox" name="pollinators-cpg">Pollinators</span>
</div>
<div class="columns">
<span><input type="checkbox" name="gas-cpg">Gas Leaks</span>
<span><input type="checkbox" name="cca-cpg">CCA</span>
</div>
</div>
<div class='spacer2'></div>
<input type="submit" value="SUBMIT">
<button id='back' type='button' onclick="backPage()">back</button>
</form>
</div>
</div>
</div>
</div>
<script>
function nextPage(){
var section1b = document.getElementById('section1b');
section1b.style.display='none';
var section2 = document.getElementById('section2');
section2.style.display='block';
}
function backPage(){
var section1b = document.getElementById('section1b');
section1b.style.display='block';
var section2 = document.getElementById('section2');
section2.style.display='none';
}
function mySubmit(x){
event.preventDefault();
var fname = document.getElementById('fname').value;
var lname = document.getElementById('lname').value;
var email = document.getElementById('email').value;
var mailformat = /^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/;
if(!email.match(mailformat))email="";
if(fname == "" || lname == "" || email == ""){
alert("Please enter name and email");
backPage();
return false;
}else{
//var myForm = document.getElementById('myForm');
//myForm.submit();
return true;
}
}
</script>
</body>
</html>
您只需要更改一行代码:-
您必须设置**<input type="text" />**
而不是<input type="email" />
因此,更新后的行将是:-
<input class='detailsC' type="text" name="email" placeholder="Email..." id='email'>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://www.sustainablewestonma.org/swag/public/css/getInvolved001.css?test1=apple">
<div id='container'>
<div id='section-container'>
<div id='section1' class='sections'>
<div id='section1b'>
<span class='majCap'>Get Involved</span>
<span class='minCap'>Enter your details below</span>
<form action='getInvolvedCtrl.php' method='POST' id='myForm' onsubmit="return mySubmit(event)">
<div class='columns'>
<input class='detailsC' type="text" name="fname" placeholder="First Name" id='fname'>
<input class='detailsC' type="text" name="lname" placeholder="Last Name" id='lname'>
<input class='detailsC' type="text" name="email" placeholder="Email..." id='email'>
</div>
<div class='rowsDet'>
<span class='detailsR'><input type="checkbox" name="wres">Weston Resident</span>
<span class='detailsR'><input type="checkbox" name="news">Quarterly Newsletter</span>
</div>
<div class='spacer'></div>
<span class="minCap">Areas of Interests</span>
<div class='rowsInt'>
<div class="columns">
<span><input type="checkbox" name="energy-int">Energy</span>
<span><input type="checkbox" name="recycling-int">Recycling</span>
<span><input type="checkbox" name="composting-int">Composting</span>
<span><input type="checkbox" name="transport-int">Transportation</span>
</div>
<div class="columns">
<span><input type="checkbox" name="climate-int">Climate</span>
<span><input type="checkbox" name="trees-int">Trees</span>
<span><input type="checkbox" name="pollinators-int">Pollinators</span>
<span><input type="checkbox" name="water-int">Water</span>
<span><input type="checkbox" name="other-int">Other</span>
</div>
</div>
<button id='next' type='button' onclick="nextPage()">next</button>
</div>
<div id='section2' class='sections'>
<span class='minCap'>Current Campaign Information:</span>
<div class='rowsCam'>
<div class="columns">
<span><input type="checkbox" name="cleanup-cpg">Town Cleanup</span>
<span><input type="checkbox" name="pollinators-cpg">Pollinators</span>
</div>
<div class="columns">
<span><input type="checkbox" name="gas-cpg">Gas Leaks</span>
<span><input type="checkbox" name="cca-cpg">CCA</span>
</div>
</div>
<div class='spacer2'></div>
<input type="submit" value="SUBMIT">
<button id='back' type='button' onclick="backPage()">back</button>
</form>
</div>
</div>
</div>
</div>
<script>
function nextPage() {
var section1b = document.getElementById('section1b');
section1b.style.display = 'none';
var section2 = document.getElementById('section2');
section2.style.display = 'block';
}
function backPage() {
var section1b = document.getElementById('section1b');
section1b.style.display = 'block';
var section2 = document.getElementById('section2');
section2.style.display = 'none';
}
function mySubmit(x) {
event.preventDefault();
var fname = document.getElementById('fname').value;
var lname = document.getElementById('lname').value;
var email = document.getElementById('email').value;
var mailformat = /^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/;
if (!email.match(mailformat)) email = "";
if (fname == "" || lname == "" || email == "") {
alert("Please enter name and email");
backPage();
return false;
} else {
//var myForm = document.getElementById('myForm');
//myForm.submit();
return true;
}
}
</script>
</body>
</html>
也可以在这里查看我的解决方案-
https://codepen.io/pen/?editors=1010