0

I have using ASP.NET MVC 5 and for validation I am using the regular Jquery unobtrusive validation library. Currently I am facing an issue, The regular expression validation is working all right on client side but on server side its not validating the value.

 [RegularExpression(@"^[0-9]{19}$", ErrorMessage = "Invalid Value")]
 public string Value{ get; set; }

this is the simple regular expression to validate 19 digits. Its working fine on client side it'll now allow ' or - or any thing to pass on but if i submit directly to server than the model is not being validated. Do I need to do some thing special?

2
  • 2
    Are you calling Model.IsValid? What happens when the model binds? Any exception? Show us something else to help. Commented Jun 10, 2014 at 17:20
  • Some how ModelState.IsValid was not returning the error. Commented Jun 10, 2014 at 17:27

1 Answer 1

0

ModelBinding ( the process of mapping post/get values to a complex model) occurs when you send data via post/get/put/delete requests, if you are manually creating a class with validation classes somewhere those data/validation annotations won't apply.

you can validate models manually - Validate list of models programmatically in ASP.NET MVC

That said, there is a library called PostSharp that can enforce those rules even when you are creating custom object/models outside of MVC ( CodeContracts ) and if it doesn't validate it throws an exception. Keep in mind that this library is paid but it has free version available that might fit your needs.

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.