ABAZE DevS Blog

La Développement une vie une Passion

APS.MVC Server sideValidating User Input with Data Annotations

Posted by abaze17 on avril 21st, 2017

You can use data annotations in MVC models to

set validation criteria for user input. Input

validation is the process by which MVC checks

data provided by a user or a web request to

ensure that it is in the right format. The following

example shows a webpage form that collects

some information from the user:

o Name. This is required input. The user

must enter some data in this field.

o Height. This must be an integer between

0 and 400.

o Email Address. This is required input. The value entered must be a valid email address.

[Required(ErrorMessage=”Please enter a name.”)]

public string Name { get; set; }

Custom validation

exemple de validation

public class CheckValidYear : ValidationAttribute

{

public override bool IsValid(object value)

{

int year = (int)value;

if (year




 


Créer un Blog | Nouveaux blogs | Top Tags | 14 articles | blog Gratuit | Abus?