.NET|.NET Core中简单的邮箱格式校验方式
目录
- Intro
- Implement
- More
- References
- 总结
Intro 前段时间有一个验证邮箱格式的小需求,然后突然发现了一种非常简单的邮箱格式判断方式
Implement 直接来看实现
public static bool IsEmailAddress(string email){if (string.IsNullOrWhiteSpace(email))return false; var symbolIndex = email.IndexOf('@'); return symbolIndex > 0&& symbolIndex < email.Length - 1&& symbolIndex == email.LastIndexOf('@'); }
在之前的认知里,一般判断邮箱格式都是用一个正则表达式,有时候正则表达式还可能会特别复杂,在老的 .NET framework 中 EmailAddress 的判断使用的是一个很复杂的一个正则表达式
const string pattern = @"^((([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])))\.?$";
可以参考:https://referencesource.microsoft.com/#System.ComponentModel.DataAnnotations/DataAnnotations/EmailAddressAttribute.cs,54而在 .NET Core 中就比较简单了,没有用到正则,前面的实现也是来自于 .NET Core EmailAddressAttribute 的实现,实现如下:
public sealed class EmailAddressAttribute : DataTypeAttribute{public EmailAddressAttribute(): base(DataType.EmailAddress){// Set DefaultErrorMessage not ErrrorMessage, allowing user to set// ErrorMessageResourceType and ErrorMessageResourceName to use localized messages.DefaultErrorMessage = SR.EmailAddressAttribute_Invalid; }public override bool IsValid(object value){if (value =https://www.it610.com/article/= null){return true; }if (!(value is string valueAsString)){return false; }// only return true if there is only 1'@' character// and it is neither the first nor the last characterbool found = false; for (int i = 0; i < valueAsString.Length; i++){if (valueAsString[i] == '@'){if (found || i == 0 || i == valueAsString.Length - 1){return false; }found = true; }}return found; }}
【.NET|.NET Core中简单的邮箱格式校验方式】通过这种方式,我们可以提高判断邮箱格式的性能又不必维护正则表达式了。总结:有且仅有一个@并且前后都有字符
More 有一点需要注意,在上面的 EmailAddressAttribute 的实现中,如果值是 null 也会认为是“合法”的,这里的“合法”并不是说邮箱格式合法而是说验证可以通过,实际情况下一般我们是会认为这并不是一个合法的邮箱
References
- https://github.com/dotnet/runtime/blob/main/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/EmailAddressAttribute.cs
- https://github.com/dotnet/runtime/blob/v5.0.0/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/EmailAddressAttribute.cs
- https://github.com/dotnet/corefx/blob/v3.0.0/src/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/EmailAddressAttribute.cs
- https://github.com/dotnet/corefx/blob/v2.2.0/src/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/EmailAddressAttribute.cs
- https://github.com/dotnet/corefx/blob/v2.0.0/src/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/EmailAddressAttribute.cs
总结 到此这篇关于.NET Core中简单的邮箱格式校验方式的文章就介绍到这了,更多相关.NET Core邮箱格式校验内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- Python中转换数据类型的函数
- Python中常用数据类型转换函数的使用方法和步骤
- 谈谈工作中的犯错
- Java8中AbstractExecutorService与FutureTask源码详解
- response文件流输出文件名中文不显示的解决
- 如何在mapper文件中使用in("str1","str2")
- API文档
- 浏览器动态显示服务器日志,基于 websocket 实现远程实时日志 在浏览器中查看设备的运行日志...
- 凯迪拉克故障u1510案例_凯迪拉克ATSL行驶中仪表板偶尔提示“请速检修车辆”
- YYC蜻蜓系统系列|点发送短信无反应是很多用户在搭建优雅草蜻蜓q系统中常见问题-解决点击发送短信无反应