| File: System\ComponentModel\DataAnnotations\EmailAddressAttribute.cs | Web Access |
| Project: src\runtime\src\libraries\System.ComponentModel.Annotations\src\System.ComponentModel.Annotations.csproj (System.ComponentModel.Annotations) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. namespace System.ComponentModel.DataAnnotations { [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] public sealed class EmailAddressAttribute : DataTypeAttribute { public EmailAddressAttribute() : base(DataType.EmailAddress) { // Set DefaultErrorMessage not ErrorMessage, allowing user to set // ErrorMessageResourceType and ErrorMessageResourceName to use localized messages. DefaultErrorMessage = SR.EmailAddressAttribute_Invalid; } public override bool IsValid(object? value) { if (value == null) { return true; } if (!(value is string valueAsString)) { return false; } if (valueAsString.AsSpan().ContainsAny('\r', '\n')) { return false; } // only return true if there is only 1 '@' character // and it is neither the first nor the last character int index = valueAsString.IndexOf('@'); return index > 0 && index != valueAsString.Length - 1 && index == valueAsString.LastIndexOf('@'); } } }