Regex to detect invalid UTF-8 string

You can use this PCRE regular expression to check for byte sequences in a string that are not valid UTF-8. If the regex matches, the string contains invalid byte sequences. It’s 100% portable because it doesn’t rely on PCRE_UTF8 to be compiled in. $regex = ‘/( [\xC0-\xC1] # Invalid UTF-8 Bytes | [\xF5-\xFF] # Invalid … Read more

ASP.NET MVC – Custom validation message for value types

Make your own ModelBinder by extending DefaultModelBinder: public class LocalizationModelBinder : DefaultModelBinder Override SetProperty: base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value); foreach (var error in bindingContext.ModelState[propertyDescriptor.Name].Errors. Where(e => IsFormatException(e.Exception))) { if (propertyDescriptor.Attributes[typeof(TypeErrorMessageAttribute)] != null) { string errorMessage = ((TypeErrorMessageAttribute)propertyDescriptor.Attributes[typeof(TypeErrorMessageAttribute)]).GetErrorMessage(); bindingContext.ModelState[propertyDescriptor.Name].Errors.Remove(error); bindingContext.ModelState[propertyDescriptor.Name].Errors.Add(errorMessage); break; } } Add the function bool IsFormatException(Exception e) to check if an Exception is a FormatException: … Read more

How do I validate email address formatting with the .NET Framework?

Don’t bother with your own validation. .NET 4.0 has significantly improved validation via the MailAddress class. Just use MailAddress address = new MailAddress(input) and if it throws, it’s not valid. If there is any possible interpretation of your input as an RFC 2822 compliant email address spec, it will parse it as such. The regexes … Read more

Valid email address regular expression? [duplicate]

You were close, have to add just a few more characters: (?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\”.\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t] )+|\Z|(?=[\[“()<>@,;:\\”.\[\]]))|”(?:[^\”\r\\]|\\.|(?:(?:\r\n)?[ \t]))*”(?:(?: \r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\”.\[\] \000-\031]+(?:(?:( ?:\r\n)?[ \t])+|\Z|(?=[\[“()<>@,;:\\”.\[\]]))|”(?:[^\”\r\\]|\\.|(?:(?:\r\n)?[ \t]))*”(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\”.\[\] \000-\0 31]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[“()<>@,;:\\”.\[\]]))|\[([^\[\]\r\\]|\\.)*\ ](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\”.\[\] \000-\031]+ (?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[“()<>@,;:\\”.\[\]]))|\[([^\[\]\r\\]|\\.)*\](?: (?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\”.\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z |(?=[\[“()<>@,;:\\”.\[\]]))|”(?:[^\”\r\\]|\\.|(?:(?:\r\n)?[ \t]))*”(?:(?:\r\n) ?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;:\\”.\[\] \000-\031]+(?:(?:(?:\ r\n)?[ \t])+|\Z|(?=[\[“()<>@,;:\\”.\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\”.\[\] \000-\031]+(?:(?:(?:\r\n) ?[ \t])+|\Z|(?=[\[“()<>@,;:\\”.\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t] )*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\”.\[\] … Read more

Open Graph validation for HTML5

For HTML5, add this to your html element like described on ogp.me and keep your og: prefixed properties: <!doctype html> <html prefix=”og: http://ogp.me/ns#”> <head> <meta property=”og:type” content=”website” /> … For XHTML (like OP’s question), use the name attribute instead of property attribute. Facebook lint will throw a warning, but the meta value will still be … Read more

Sql script to find invalid email addresses

SELECT * FROM people WHERE email NOT LIKE ‘%_@__%.__%’ Anything more complex will likely return false negatives and run slower. Validating e-mail addresses in code is virtually impossible. EDIT: Related questions I’ve answered a similar question some time ago: TSQL Email Validation (without regex) T-SQL: checking for email format Regexp recognition of email address hard? … Read more

Validate Enum Values

You got to love these folk who assume that data not only always comes from a UI, but a UI within your control! IsDefined is fine for most scenarios, you could start with: public static bool TryParseEnum<TEnum>(this int enumValue, out TEnum retVal) { retVal = default(TEnum); bool success = Enum.IsDefined(typeof(TEnum), enumValue); if (success) { retVal … Read more