Santosh
Santosh

Reputation: 2495

validations in MVC2/MVC3 with out DataAnnotations

can we validate a form which has few textboxes and i want to validate them for require field etc. by using javascriptfunctions in the samepage(.aspx/.cshtml).

i dont want to use DataAnnotations in Model.

Upvotes: 0

Views: 633

Answers (2)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93424

Only validating on the client is a bad idea, and insecure. You need to ALWAYS do server side validation, and only do client-side validation as a "nice to have". Otherwise validation can be bypassed by any attacker just by bypassing the client-side script.

There are several ways to do server (and client) side validation without data annotations. You can use Metadata "buddy" classes and apply the attributes to the buddy class. Or you can use Fluent Validation. You can also use IValidatableObject. You really really really should use some kind of server side validation though.

Upvotes: 1

Shyju
Shyju

Reputation: 218722

In mvc 3, With Model DataAnnotations You can do javascript validations in client side. It uses jquery validate script to do the validations.

in ASP.NET MVC 3 Beta, we’ve updated the runtime to enable a feature we’re calling “Unobtrusive Client Validation”. We have also created a consumer for these unobtrusive client validation attributes that uses jQuery and jQuery Validate to perform the validation on our behalf.

http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html

You should never rely on client side validation only. Do both client side and server side. What if somebody is accessing your website from a javascript disabled browser ?

Upvotes: 0

Related Questions