1

I have a field that has required validation property and also remote validation property.

I want to display the error message only if remote validation fails, and not on required property.

I can set the error message to an empty string, but i don't want to modify the ViewModel Data Annotations.

Is possible to do this only from client-side, without modifying the ViewModel properties?

// working hack
public class CreateViewModel
{
    [Required(ErrorMessage = " ")]
    [Remote("IsUserNameValid", "Users", ErrorMessage = "This user name is already used")]
    [Display(Name = "User Name")]
    public string UserName { get; set; }
}

1 Answer 1

2

You could disable the required rule. For example given a form with id "LoginForm" and a required input field with id "UserName", you can disable the required rule using either of these:

$("#UserName").rules("remove", "required");
$("#loginForm").validate().settings.rules["UserName"].required= false;

Another option is to change the error message to an empty string. Again you have a few options, for example:

$("#UserName").rules("add", {
    messages: {
        required: ""
    }
});
$("#loginForm").validate().settings.messages["UserName"].required= '';

In the end this is about playing with the rules and messages of the jquery validation object for the form.

Hope it helps!

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

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.