Reputation: 704
I've successfully made a working jQuery.validation form for my site in just HTML, CSS and JS (with dependencies: jQuery and Validation). The problem is when I'm in a clean ASP.NET Core web project and I use the exact same code, it just doesn't seem to use my custom validation parameters in JavaScript
The validation works in all cases. However, my custom [options]
for .validate([options])
don't seem to work on ASP.NET Core
Example of the working code: JSFiddle
HTML:
<form method="post">
<fieldset>
<div class="master">
<input name="name" id="name" placeholder="Name" minlength="2" maxlength="40" required />
</div>
<div class="master">
<input name="email" id="email" placeholder="Email Address" type="email" required />
</div>
<div class="master">
<input name="company" placeholder="Company Name" />
</div>
<div class="master">
<input name="website" placeholder="Website" type="url" required />
</div>
<button type="submit">Next</button>
</fieldset>
</form>
CSS:
.master {
padding: 10px;
background-color: white;
}
.has-error {
background-color: red;
}
.has-success {
background-color: green;
}
JS:
$('form').validate({
// This disables the default validation notifications to the user.
// Instead I'll be using CSS colors to notify users of valid or invalid fields
errorPlacement: function (error, element) { },
// Invalid field produces a red-background in parent element
highlight: function(element) {
$(element).parent().addClass('has-error').removeClass('has-success');
},
// Valid field produces a green-background in parent element
unhighlight: function(element) {
$(element).parent().removeClass('has-error').addClass('has-success');
}
});
My JavaScript .validate([options])
does 3 things:
<field>
is INVALID, parent element background-color is red<field>
is VALID, parent element background-color is greenNote:
My conclusion: The validation works in all cases, which means both ASP.NET Core applications and plain HTML - CSS - JS have properly functional validation.
The only thing that doesn't work is when I provide [options]
in my $('form').validate([options]);
in a ASP.NET Core application. I'm not sure why my options aren't being used.
Official jQuery.validate page on .validate([options])
Upvotes: 2
Views: 4035
Reputation: 704
I figured it out the problem. It had to do with jQuery.validate([option]
) object property: success
It directly conflicts with two other properties: highlight
and unhighlight
For example:
$('form').validate({
errorPlacement: function (error, element) { },
highlight: function (element) {
// Do stuff
},
unhighlight: function (element) {
// Do stuff
}
success: function (element) {
// Do stuff
}
});
The success
property completely uses a different way of notifying the user of invalid fields via tooltips. Where as highlight
and unhighlight
has more of a custom way for the developer to have more control.
In ASP.NET Core since jQuery.validate comes shipped with every ASP.NET Core Web Application, it applies its own success
property as apart of its scaffolding somewhere within build.
The way I got around this is by explicitly calling my JS script last and placing a callback to any function within the success
function.
Upvotes: 1