.net - EmailAddressAttribute without being required -


i have [emailaddress] dataannotation .net 4.5 on model property, returns 'the email field not valid e-mail address.' error during validation, when email property empty.

while technically true, have expected empty value caught [required] annotation.

is there parameter i'm missing can passed [emailaddress] annotation allow empty strings validate, or have fall using custom validator regular expression?

there no such parameter emailaddressattribute. validation code attibute following:

if (value == null) {     return true; } string input = value string; return ((input != null) && (_regex.match(input).length > 0)); 

and regex defined (there no match empty string):

_regex = new regex(@"^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(([a-z]|\d|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])([a-z]|\d|-|\.|_|~|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])*([a-z]|\d|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])))\.)+(([a-z]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(([a-z]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])([a-z]|\d|-|\.|_|~|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])*([a-z]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])))\.?$", regexoptions.compiled | regexoptions.explicitcapture | regexoptions.ignorecase); 

to achieve goal can create can inherit datatypeattribute override isvalid method, return true empty or null strings , use instance of emailaddressattribute. should this:

public override bool isvalid(object value) {     if (value == null)     {         return true;     }     string input = value string;     var emailaddressattribute = new emailaddressattribute();      return (input != null) && (string.isnullorempty(input) || emailaddressattribute.isvalid(input)); } 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -