IamaC IamaC - 1 month ago 6
ASP.NET (C#) Question

MVC3 Validation Error Message in Tooltip

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

click here

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?

Answer

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.