[英]First check for errors before submitting
I have a form which I want it to be verified and validated by a try-catch block plus two other functions. 我有一个表单,希望通过try-catch块以及其他两个功能对其进行验证。 Here is what I have: HTML:
这是我所拥有的: HTML:
<h5>Please enter your name below</h5>
<form name="textForm">
<label for="name">First name:</label>
<input type="text" name="fname" id="name">
</form>
<h5>Please enter your e-mail below</h5>
<form name="mailForm">
<label for="email">Email:</label>
<input type="email" name="email" id="email">
</form>
<form action="">
<label for="height">Your height:</label>
<input type="text" placeholder="in centimetres" name="personHeight" id="height" />
<br></br>
<input formaction="mailto:test@gmail.com" onclick="heightCheck();validateTextForm();validateMailForm();" type="submit" value="Submit all" id="submit" method="post" formtarget="_blank" />
<br></br>
<p id="mess"></p>
</form>
JS : JS :
function validateTextForm() {
var x = document.forms["textForm"]["fname"].value;
if (x == null || x == "") {
alert("First name must be filled out");
return false;
}
}
function validateMailForm() {
var z = document.forms["mailForm"]["email"].value;
var atpos = z.indexOf("@");
var dotpos = z.lastIndexOf(".");
if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= z.length) {
alert("Not a valid e-mail address");
return false;
}
}
function heightCheck() {
try {
var x = document.getElementById("height").value;
if (x == "")
throw "enter your height";
if (isNaN(x)) throw "not a number";
if (x > 250)
throw "height is too high";
if (x < 80)
throw "height is too low";
} catch (err) {
var y = document.getElementById("mess");
y.innerHTML = "Error: " + err + ".";
}
}
The thing that i want it to happen is the following: Firstly, it does the form validation but afterwards if its correct, submits as well. 我希望它发生的事情如下:首先,它执行表单验证,但是如果它正确,则也提交它。 I tried to make it first verify the forms before actually submits but without any success.
我试图使其首先在实际提交之前验证表格,但没有成功。 While browsing i find out it could be done by either
stopPropaganation
, preventDefault
or return false
methods but still have no idea how to make it happen. 在浏览时,我发现可以通过
stopPropaganation
, preventDefault
或return false
方法来完成,但仍然不知道如何实现。 I will rep the guys who help me. 我会代表帮助我的家伙。 Thanks in advance!
提前致谢! Fiddle: However in fiddle it doesn't run properly as it should: http://jsfiddle.net/5T9sJ/
小提琴:但是,小提琴无法正常运行: http : //jsfiddle.net/5T9sJ/
You need to change your code according to my editions: 您需要根据我的版本更改代码:
...
<input formaction="mailto:test@gmail.com" type="submit" value="Submit all" id="submit" method="post" formtarget="_blank" />
...
Then make processValidate
method to wrap all other validations into it: 然后使用
processValidate
方法将所有其他验证包装到其中:
$(document).ready(function () {
$('input#submit').click(function (e) {
e.preventDefault();
validateTextForm();
validateMailForm();
heightCheck();
});
function validateTextForm() {
var x = document.forms["textForm"]["fname"].value;
if (x === null || x === "") {
alert("First name must be filled out");
return false;
}
}
function validateMailForm() {
var z = document.forms["mailForm"]["email"].value;
var atpos = z.indexOf("@");
var dotpos = z.lastIndexOf(".");
if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= z.length) {
alert("Not a valid e-mail address");
return false;
}
}
function heightCheck() {
try {
var x = document.getElementById("height").value;
if (x === "") throw "enter your height";
if (isNaN(x)) throw "not a number";
if (x > 250) throw "height is too high";
if (x < 80) throw "height is too low";
} catch (err) {
var y = document.getElementById("mess");
y.innerHTML = "Error: " + err + ".";
}
}
});
Explanation: 说明:
You have several methods bound on submit input type, so clicking on it will always call form submission. 您在提交输入类型上有几种方法,因此单击它将始终调用表单提交。
I added jQuery click
handler for this action to prevent the default action of submit button. 我为此操作添加了jQuery
click
处理程序,以防止提交按钮的默认操作。
I think e.preventDefault()
suits here the best. 我认为
e.preventDefault()
最适合这里。
WORKING DEMO: http://jsfiddle.net/rbegf/ 工作演示: http : //jsfiddle.net/rbegf/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.