1

i am new in mvc .here scott shows how to Creating a Custom [Email] Validation Attribute in mvc. here is the picture.

enter image description here enter image description here

http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

1) now see how they did it. first create a class give a name and extend regular expression attribute class and in its ctor they use regex to validate email address

my question is when they use [Email(Errormessage="blah blah")]

then how MVC can understand this email attribute is pointing to email attribute class which extend regularexpression attribute class. how relation will be extanlish. the class name is email attribute but when they use then they use attribite name email. this is not clear to me please explain.

2) if i validate the email the above way they where validation will occur means at server side or client side ?

if not client side then how can i make it client and required js will be render for that.

please explain me with sample code example. thanks

Thomas
  • 33,544
  • 126
  • 357
  • 626
  • 1
    It's unobtrusive javascript validation. [This](http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html) explains it quite well – Liam Sep 13 '13 at 14:48
  • http://stackoverflow.com/questions/5630424/asp-net-mvc-disable-client-side-validation-at-per-field-level – Mou Sep 13 '13 at 18:49

1 Answers1

0

The first question is best answered with a principle widely used in MVC: convention over configuration. That basically means: do the less config possible, use the most default functionalities. Several examples in ASP.NET MVC

  • Folder Controllers contain controllers by default.
  • The name of a view corresponds to the name of a Action in a Controller.
  • The folder name where a view is located corresponds to the Controller name without 'Controller' ending.
  • The class name of the controller ends with 'Controller' which is omitted when calling the controller.
  • The same with Attributes; the class name ends with 'Attribute' which is omitted in usage
  • etc, etc, etc,

There are many more like this and it is not configured. It is convention.

The second question is already partially answered in the question itself: you cannot inherit from EmailAddressAttribute as it's a sealed class. But you can use RegularExpressionAttribute the way it's described in your question, or create a new attribute, like I will do it below.

However this way the validation will take place only on server side. To make it on client side you need to do the following:

public class EmailAttribute : ValidationAttribute, IClientValidatable
{
    private const string VALIDATION_TYPE = "customEmail";
    private const string EMAIL_REGEX = @"put your regex here";        

    public virtual IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        yield return new ModelClientValidationRule { ValidationType = VALIDATION_TYPE, ErrorMessage = ErrorMessageString };
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var sValue = value as string;

        if (!string.IsNullOrEmpty(sValue) && Regex.Match(sValue, EMAIL_REGEX).Success)
        {
            return ValidationResult.Success;
        }

        return new ValidationResult(string.Format(ErrorMessageString, validationContext.MemberName));
    }
}

Then in Javascript (I suppose you've included jQuery, jQuery.validate and jQuery.validate.unobtrusive) use the following:

$.validator.addMethod('customEmail', function (value, element) {
    let regex = /put your regex here/;
    return regex.test($(element).val());
});

$.validator.unobtrusive.adapters.add('customEmail', [], function (options) {
    options.messages['customEmail'] = options.message;
    options.rules['customEmail'] = options.params;
});
Pavel Jounda
  • 219
  • 5
  • 14