bootstrapValidator 邮箱和URL验证规则

2021-10-07 From 程序之心 By 程序之心

使用 bootstrapValidator 可以很方便地做表单验证。但是,后端也需要兜底,要和前端保持相同规则。扒了下 bootstrapValidator 的源码,找到邮箱和URL的验证规则如下,方便后端参考。

邮箱:

var emailRegExp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/

URL:

var urlExp = new RegExp(
    "^" +
    // protocol identifier
    "(?:(?:" + protocol + ")://)" +
    // user:pass authentication
    "(?:\\S+(?::\\S*)?@)?" +
    "(?:" +
    // IP address exclusion
    // private & local networks
    (allowLocal
        ? ''
        : ("(?!(?:10|127)(?:\\.\\d{1,3}){3})" +
            "(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})" +
            "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})")) +
    // IP address dotted notation octets
    // excludes loopback network 0.0.0.0
    // excludes reserved space >= 224.0.0.0
    // excludes network & broadcast addresses
    // (first & last IP address of each class)
    "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
    "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
    "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
    "|" +
    // host name
    "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
    // domain name
    "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
    // TLD identifier
    "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
    // Allow intranet sites (no TLD) if `allowLocal` is true
    (allowLocal ? '?' : '') +
    ")" +
    // port number
    "(?::\\d{2,5})?" +
    // resource path
    "(?:/[^\\s]*)?" +
    "$", "i"
);

本文来源:程序之心,转载请注明出处!

君子曰:学不可以已。
《知识图谱:方法、实践与应用》

知识图谱是较为典型的多学科交叉领域,涉及知识工程、自然语言处理、机器学习、图数据库等多个领域。《知识图谱:方法、实践与应用》系统地介绍知识图谱涉及的关键技术,如知识建模、关系抽取、图存储、自动推理、图谱表示学习、语义搜索、知识问答、图挖掘分析等。此外,本书还尝试将学术前沿和实战结合。

发表感想

© 2016 - 2024 chengxuzhixin.com All Rights Reserved.

浙ICP备2021034854号-1    浙公网安备 33011002016107号