【问题标题】:Address validation restrict more than 3 special character地址验证限制超过 3 个特殊字符
【发布时间】:2018-06-08 10:39:40
【问题描述】:

我正在验证地址字段,我需要一些验证正则表达式来拒绝超过 3 个特殊字符(逗号)

有效格式

New York Marriott Marquis, New York, NY, USA

格式无效

New York,# Marriott, Marquis,& New York, NY, USA,
$$$^^^&&&&&&,&&&&&########New York, NY, USA

preg_match('/^[^*|\":<>[\]{}`\\()';@&$]+$/');

preg_match('/[*|\":<>[\]{}`\\()';@&$]/');

preg_match('/^[a-z0-9- ]+$/i');

但所有这些都没有按我的需要工作。

我试过了 任何帮助将不胜感激。 在此先感谢:)

【问题讨论】:

  • 你能分享你的尝试吗?
  • 当然@Thefourthbird 会立即编辑问题。
  • 超过 3 个逗号或超过 $^&amp;, 的总和
  • 好的,我会试试的。谢谢:)
  • 超过 3 个逗号或超过 $^&amp;, 中的任何一个,例如 ^(?!(?:.*[$^&amp;,]){4,}).+$

标签: php regex validation


【解决方案1】:

尝试使用否定前瞻(?! 断言后面的内容与character class 中的任何字符都不匹配4 次:

^(?!(?:.*[*|\":[\]{}`\()';@&$,]){4}).+$

Regex test

$str = 'New York Marriott Marquis, New York, NY, USA';
preg_match('/^(?!(?:.*[*|\":<>[\]{}`\()\';@&$,]){4}).+$/', $str, $matches);
var_dump($matches);

Php test

【讨论】:

    猜你喜欢
    • 2019-06-24
    • 2013-05-16
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多