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; }
}
Catalin
  • 11,503
  • 19
  • 74
  • 147

1 Answers1

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!

Community
  • 1
  • 1
Daniel J.G.
  • 34,266
  • 9
  • 112
  • 112