使用pattern属性排除邮政信箱地址 [英] Excluding PO Box Addresses using the pattern attribute

查看:37
本文介绍了使用pattern属性排除邮政信箱地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我知道这里已经有一些关于排除邮政信箱地址的线程,但是似乎大多数线程都使用Jquery,而我在自己的平台(prestashop)中复制它们的尝试还没有解决为我.如果所有其他方法都失败了,虽然这可能是我的解决方案,但我希望尽可能使用模式输入属性.

First off I'm aware that there are already a few threads on here regarding excluding PO Box addresses but it seems that the majority of them use Jquery and my attempts to replicate them within my platform (prestashop) haven't worked out for me. While this may be the solution I resort to if all else fails I'm hoping to use the pattern input attribute if it's possible.

我想在pattern属性中使用一个正则表达式,但是我对正则表达式不太熟悉.实际上,我只是最近才遇到他们.

I've got a regular expression that I wish to use within the pattern attribute however I'm not too familiar with regular expressions. In fact I've only recently come across them.

我正在根据是否已输入邮政信箱地址来验证表单.如果有,则该表单将不会提交.经过研究后,我发现以下表达式可以满足我的需求:

I'm looking to validate a form based on whether a PO Box address has been inputted. If it has then the form will not submit. After doing some research I've come across the following expression which is said to do what I'm looking for:

^ *((#\d+)|((box|bin)[-. \/\\]?\d+)|(.*p[ \.]? ?(o|0)[-. \/\\]? *-?((box|bin)|b|(#|num)?\d+))|(p(ost)? *(o(ff(ice)?)?)? *((box|bin)|b)? *\d+)|(p *-?\/?(o)? *-?box)|post office (box|bin)|((box|bin)|b) *(number|num|#)? *\d+|(num|number|#) *\d+)

这个表达式对于我的理解来说太复杂了,但是在Regex101中查看它似乎与我正在寻找的相反,我可能会误会,但它似乎是在验证邮政信箱并排除其他所有内容.无论如何,我都在表单上尝试过它,但是它看起来确实如此,因此无论输入什么内容,都不会被排除在外.

This expression is far too complex for my understanding but looking at it in Regex101 it seems to be doing the opposite of what I'm looking for, I could be misunderstanding but it seems to be validating PO boxes and excluding everything else. Regardless of that I tried it on my form but it seems to make it so that regardless of what is inputted it wont be excepted.

这使我相信我做错了什么,或者模式输入字段不支持这种复杂的表达式,或者如果需要,则需要使用其他语法.

This has lead me to believe that either I've done something wrong or the pattern input field doesn't support such complex expressions or it needs to be in a different syntax if it does.

我希望对此事进行一些澄清,并建议采取一些步骤来验证我的表格.

I'd appreciate some clarification on that matter and recommended steps to take in validating my forms.

模式输入属性是否支持这种类型的表达式,如果不支持,我该如何使用它来验证表单?

为了澄清起见,此表达式会验证邮政信箱还是将其排除,如果能验证邮政信箱,我将如何更改邮政信箱以做相反的事情?

And for clarification does this expression validate PO boxes or exclude them and if it validates them how would I go about changing it to do the opposite?

这是我当前正在使用的代码:

Here is the code I'm currently using:

<label for="return-street-address">Street Address <span class="asterisk">*</span></label> <input type="text" value="" name="ADDRESS" required id="return-street-address" "^(?! *(#\d+|(box|bin)[-. /\\]?\d+|.*p[ .]? ?[o0][-. /\\]? *-?((box|bin)|b|(#|num)?\d+)|p(ost)? *(o(ff(ice)?)?)? *((box|bin)|b)? *\d+|p *-?/?o? *-?box|post office (box|bin)|((box|bin)|b) *(number|num|#)? *\d+|(num|number|#) *\d+)).*"> 

这些:

PO Box 123
P.O. BOX 123

不应使用此代码进行验证.

should not get validated with this code.

推荐答案

如果要使用找到的模式,则需要删除字符类内部不必要的转义符,并用一个字符类替换每个字母,包括低位和低位字符.字母的大写变体(这是必要的,因为不能通过HTML5模式传递正则表达式修饰符).

If you want to use the pattern you found, you need to remove unnecessary escapes inside character classes, and replace each letter with a character class including the lower- and uppercase variant of the letter (it is necessary since one cannot pass a regex modifier with an HTML5 pattern).

使用

input:valid {
  color: black;
}
input:invalid {
  color: red;
}

<label for="return-street-address">Street Address <span class="asterisk">*</span></label> <input type="text" value="" name="ADDRESS" required id="return-street-address" pattern="^(?! *(#\d+|([bB][oO][Xx]|[Bb][Ii][Nn])[-. /\\]?\d+|.*[pP][ .]? ?[Oo0][-. /\\]? *-?(([bB][oO][Xx]|[Bb][Ii][Nn])|b|(#|[Nn][Uu][Mm])?\d+)|[Pp]([Oo][Ss][Tt])? *([Oo]([Ff]{2}([Ii][Cc][Ee])?)?)? *(([bB][oO][Xx]|[Bb][Ii][Nn])|b)? *\d+|[Pp] *-?/?[Oo]? *-?[bB][oO][Xx]|[Pp][Oo][Ss][Tt] [Oo][Ff][Ff][Ii][Cc][Ee] ([bB][oO][Xx]|[Bb][Ii][Nn])|(([bB][oO][Xx]|[Bb][Ii][Nn])|[Bb]) *([Nn][Uu][Mm][Bb][Ee][Rr]|[Nn][Uu][Mm]|#)? *\d+|([Nn][Uu][Mm][Bb][Ee][Rr]|[Nn][Uu][Mm]|#) *\d+)).*"> 

这篇关于使用pattern属性排除邮政信箱地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆