0

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?

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.