Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

c# - ASP.Net Core client-side validation: is there a "validation succeeded" DOM event?

I have an ASP.Net Core Razor page that uses Bootstrap and does client-side validation.

The form looks like this:

<form method="post" class="needs-validation" novalidate>
...

_ValidationScriptsPartial.cshtml looks like this:

<environment include="Development">
    <script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
    <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
    ...

Everything works fine. If the user fails to enter a required field or enters a number that exceeds the maximum, the form won't submit and the offending field is highlighted in red.

Q: Is there a DOM event triggered on "validation succeeded"?

I would like to execute some Javascript after I know the form is "OK", but before it's actually POSTED to the server.

Thank you in advance!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want a quick turn-key solution, you can simply include the jQuery Validation Plugin. The valid condition returns true only if its valid.

Answer: you can then rxJs-subscribe to it by sending the data structure or the control in to get the event out using this jquery.observable plugin

Below is how the Jquery Valid works:

Form:


$("form").valid(); 
// or
if($("form").valid()) {
 //VALID continue
}

Control:

$("[name='CountyOfBirth']").valid(); // RETURNS true/false

On Submit: you can also get similar results of the valid event by checking on form submit

$("form").submit(function() {
    if($(this).valid()) {
       //VALID
    } else {
       //ERROR
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...