[英]How check if html5 form validation is complete
I have a form which am using html5 to validate for email and required fields.我有一个表单,它使用 html5 来验证电子邮件和必填字段。 The form is to be submitted via ajax.
表单将通过ajax提交。 How do i determine that the form passed validation before i allow to form to submitted via ajax.
在我允许通过ajax提交表单之前,我如何确定表单通过了验证。
This is my html:这是我的 html:
<form action="" method="post" id="comment_form">
<input type="hidden" name="post_id" value="<?=$post->ID ?>" />
<label for="name">Name</label>
<input type="text" name="name" required="required">
<label for="email">Email Address</label>
<input type="email" name="email" required="required">
<label for="comment">Comment</label>
<textarea name="comment" required="required" >
</textarea>
<label for="submit"></label>
<button type="submit"class="button green meduim" id="comment_btn">Submit</button>
</form>
This is my jquery这是我的 jquery
$("#comment_btn").click(function(e){
//the request url
var url = site_url+'comments/post_comment';
//cross site request forgery token
var xtoken = $("input[name='xtoken']").val();
//post data
var dataObj = {
"name":$('#name').val(),
"email":$("#email").val(),
"comment":$("#comment").val(),
"post_id":$("input[name='post_id']").val(),
"xtoken":xtoken
};
//dim form
$('#comment_form').addClass('dim');
//make request
$.ajax({
url:url,
type:'POST',
data:dataObj,
dataType:'json',
error:function() {},
success:function(resp) {
if(resp.status == "ok")
{
$('#comment_form').removeClass('dim');
}else {
alert("An error was encountered");
}
}
});
e.preventDefault();
});
Is there a way to determine if the form is validated?有没有办法确定表单是否经过验证?
Don't bind to the click event of the submit button simply bind to the submit event of your form:不要绑定到提交按钮的点击事件,只需绑定到表单的提交事件:
$('#comment_form').submit(function(e){
e.preventDefault();
//submit via ajax
});
It's probably best not to rely on the browser to validate your data, sure using required and pattern is cool and all but i wouldn't depend on it.最好不要依赖浏览器来验证您的数据,确保使用required和pattern很酷,但我不会依赖它。
Write your own validation into your javascript before the data is sent off to your URL.在将数据发送到您的 URL 之前,将您自己的验证写入您的 javascript。 You could do this with a bit of Regex or you could use many of the jQuery validation plugins available out there such as h5validate or Validation .
您可以使用一些Regex来完成此操作,也可以使用许多可用的 jQuery 验证插件,例如h5validate或Validation 。
您可以注册标准表单“onsubmit”事件,如果表单成功通过验证并正在提交,则该事件将被触发。
document.getElementById("comment_form").onsubmit = myPassedValidationHandler;
The better way I use and work for me is this one:我使用和为我工作的更好方式是这样的:
JQuery :查询:
$(document).on('submit', '#address-form', function(event){
event.preventDefault();
// submit via Ajax
});
This way is for prevent submit and send form via ajax , regards!这种方式是为了防止通过ajax提交和发送表单,问候!
It's been long, but here is my aproach to this kind of situation.已经很长时间了,但这是我对这种情况的处理方法。 Especially when i prefer to use AJAX with native HTML validation.
特别是当我更喜欢使用带有原生 HTML 验证的 AJAX 时。
document.getElementById("myBtn").addEventListener("click", checkIfValid);
const checkIfValid = {} => {
if(document.getElementById('myForm').checkValidity()) {
event.preventDefault();
// Do some AJAX stuff;
}
}
PS: If
event.preventDefault()
is used outsideif
clause, it doesn't do the trick.PS:如果
event.preventDefault()
在if
子句之外if
,它不会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.