This is a migrated thread and some comments may be shown as answers.

EmailAddress data annotation validation

1 Answer 330 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Simon
Top achievements
Rank 1
Simon asked on 18 Apr 2018, 12:55 PM

Hi there 

I have a model with a property marked up like this.

[Required]
[EmailAddress]
[Display(Name = "Username", ResourceType = typeof(MyResource))]
public string Username { get; set; }

 

The display name of the username property maps to a string "Email".

When i leave the field in the grid blank and update the row, a validation message is shown that correctly says "Email is required."
If i provide an invalid email address and update the row, a validation message is shown that incorrectly says "Username is not valid email"

I have tried applying my own validation message to the Username property with the following modifications to the model.

[Required]
[EmailAddress(ErrorMessageResourceName = "InvalidEmail", ErrorMessageResourceType = typeof(MyResource))]
[Display(Name = "Username", ResourceType = typeof(MyReouce))]
public string Username {get;set;}

 

Both of the above scenarios produce the same results with the above modifications.

What should the expected behavior be for the email validation message? (e.g use property name or display name attribute)

How do i provide a custom message for the email validation?

1 Answer, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 20 Apr 2018, 07:26 AM
Hello Simon,

By default the EmailAddress attribute will validate it the user input is a valid email. This is why validation fires when only characters are entered. If you would like to customize the message of the error I would suggest defining an ErrorMessage for the validation attribute. Furthermore, check out the stackoverflow thread below that discusses customizing validation messages.



Regards,
Viktor Tachev
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Simon
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Share this question
or