Reputation: 377
I am new to ASP.NET and MVC3. I have got a project where the MVC3 Validation Error Message needs to be shown in Tooltip without updating jquery.validate.unobtrusive.js. I have tried this
But could not get it to work. Here is the function the I should be using according to the link above. It says do something here...I could not figure out what code I should write to show the error message with a error icon beside the required field when validation fails.
$(function() {
var settngs = $.data($('form')[0], 'validator').settings;
var oldErrorFunction = settngs.errorPlacement;
var oldSucessFunction = settngs.success;
settngs.errorPlacement = function (error, inputElement) {
//Do something here
oldErrorFunction(error, inputElement);
}
settngs.success = function (error) {
//Do something here
oldSucessFunction(error);
}
});
Can anyone help?
Upvotes: 0
Views: 5422
Reputation: 1404
You can use the following css,
.myfield-validation-error
{
content: "";
display: inline-block;
height: 16px;
width: 16px;
margin-right: 4px;
background-image:url(../../Images/Close-2-icon.png);
}
.myfield-validation-valid
{
content: "";
display: inline-block;
height: 16px;
width: 16px;
margin-right: 4px;
background-image:url(../../Images/Ok-icon.png);
}
Then you can make use your these classes on your success and failure callback,
<script>
$(function () {
var settngs = $.data($('form')[0], 'validator').settings;
var oldErrorFunction = settngs.errorPlacement;
var oldSucessFunction = settngs.success;
settngs.errorPlacement = function (error, inputElement) {
$(inputElement).closest('div').next().removeClass('myfield-validation-valid').addClass('myfield-validation-error')
oldErrorFunction(error, inputElement);
}
settngs.success = function (error) {
$(error).closest('div').addClass('myfield-validation-valid').removeClass('myfield-validation-error')
oldSucessFunction(error);
}
});
</script>
Further check this link.
Upvotes: 5
Reputation: 2780
If you're using MVC3, you can leverage the validation provided. Take a look at the Account Controller - Register View for an example of how to implement it.
Research (in View):
@Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.")
and the Class for:
System.ComponentModel.DataAnnotations.RequiredAttribute
Upvotes: 1