admin管理员组文章数量:1391934
Unobtrusive validation is based on the idea that you don't do form validation until the form has been submitted by the user; once that's happened, if something is invalid on the form, then each field is immediately validated once the user has changed it.
What I want to do is trigger validation on a form element "unobtrusively" - that is, only validate the form element if the user has already tried to submit the form. So I can trigger the validation of an element (I do it when a certain checkbox is changed) like so:
$('#chkNoPersonId').change(function(){
$('#lstPersonId').valid();
});
But the trouble is that that will always cause lstPersonId
to be validated and an error displayed when invalid, even if the user hasn't yet submitted the form once. I want it to only be validated once the user has tried to submit the form. Is there some value I can check to see whether the user has tried to submit the form yet, or some other way I can achieve this behaviour?
Unobtrusive validation is based on the idea that you don't do form validation until the form has been submitted by the user; once that's happened, if something is invalid on the form, then each field is immediately validated once the user has changed it.
What I want to do is trigger validation on a form element "unobtrusively" - that is, only validate the form element if the user has already tried to submit the form. So I can trigger the validation of an element (I do it when a certain checkbox is changed) like so:
$('#chkNoPersonId').change(function(){
$('#lstPersonId').valid();
});
But the trouble is that that will always cause lstPersonId
to be validated and an error displayed when invalid, even if the user hasn't yet submitted the form once. I want it to only be validated once the user has tried to submit the form. Is there some value I can check to see whether the user has tried to submit the form yet, or some other way I can achieve this behaviour?
- 1 altough I don't get your question right I try to guess. There is a submit() eventhandler which can be used like this: $('#myForm').submit(function(){ //Do stuff once the form has been submitted }); – thpl Commented May 24, 2013 at 11:49
2 Answers
Reset to default 5You can add a flag on submit button to identify form submit has been already clicked or not. ex:
$('#submitButn').click(function(){
$(this).attr('data-submitted','true');
});
Than in each validation of input check weather that flag is true or not and perform your validation logic.
$('#chkNoPersonId').change(function(){
if( $('#submitButn').attr('data-submitted')=='true'){
$('#lstPersonId').valid();
}
});
On clear or reset of form you can remove that attribute
$('#submitButn').removeAttr('data-submitted');
Have you tried using using the submit
event handler?
$("form").submit(function() {
//validate here
});
source: http://api.jquery./submit/
本文标签: javascriptTrigger jQuery form validation only if form has already been submittedStack Overflow
版权声明:本文标题:javascript - Trigger jQuery form validation only if form has already been submitted? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744725451a2621935.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论