0

i am using asp.net mvc data validations with jquery Unobtrusive for validating data.

public class Register { [Required] public string Name { get; set; } [Required(ErrorMessage = "must not be empty")] public string Surname { get; set; } [EmailAddress(ErrorMessage = "please enter a valid email address")] [Required] public string Email { get; set; } } 

Before form post, jquery checks the validation and lists errors on validation summary area.

My problem is;

i don't want to display errors on required attributes, i just want to paint the textarea or whatever to red - which is default behaviour with error messages.

To hide error list i used

.validation-summary-errors ul{ display: none; } 

this css but it hides all of the validation errors and not just required ones. How can i achieve displaying all errors except required ones?

Note:

[Required(ErrorMessage = null)] 

or

[Required(ErrorMessage = "")] 

doesn't work

5
  • if you have @Html.ValidationSummary() in your page remove it and try.CommentedDec 10, 2013 at 13:02
  • it removes validation errors completely i want to remove just required attribute errors
    – bahadir
    CommentedDec 10, 2013 at 13:08
  • Just before return View() call, manually remove errors from ModelState.
    – Mat J
    CommentedDec 10, 2013 at 13:56
  • @Mathew Unobtrusive validation will add those errors before a POST back to the server. He'll never get to the ModelState because JQuery will notice it before it submits the form and stop it.CommentedDec 10, 2013 at 14:01
  • @BoredBlazer Oh yes. I didn't thought about that. my bad.
    – Mat J
    CommentedDec 10, 2013 at 14:16

2 Answers 2

1

That is the default behavior of the unobtrusive validation. if you want it to behave differently then you have create your Custom Attribute according to your requirement.

This link has the example for creating the custom validations attribute http://jqueryvalidationunobtrusivenative.azurewebsites.net/AdvancedDemo/CustomValidation

hope this helps.

1
  • thank you, now i can add empty error messages to my viewmodel with custom attributes but i still cant prevent jquery to add empty li elements to error list. any idea about this? or do i need to modify jquery unobtrusive source?
    – bahadir
    CommentedDec 10, 2013 at 15:22
0

this could work

[Required(ErrorMessage = " ")]

(I added a space). The validation will work, but the message will be empty (a space)

1
  • Unobtrusive validation displays the errors in an unordered list so it'll leave him with blank lines at random intervalsCommentedDec 10, 2013 at 13:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.