1

我查看了 MSDN 和谷歌搜索,但找不到我应该如何解释这个表达式......

这是一个地址参数....

^[a-zA-Z\\\d/\\\\\\\\]{0,80}$

我知道任何大写/小写字母、数字都是允许的,并且最多允许 80 个字符。

我不明白这4个\是干什么用的。

我希望这个正则表达式允许特殊字符,因为它是表单的街道地址输入字段。例如,我希望这表示可以将 2400 Tam O'Shanter Place 或 1904 Billings-Harper Terrace 输入该字段。

4

1 回答 1

0

There are 5 redundant \ characters (which are all escaped by \). You only need one (escaped, of course) to allow a literal \ to be entered.

That said, this looks like a terrible way to validate street addresses:

  • It doesn't allow hyphens, which are common (eg, 101-204 Main st) or apostrophes (eg O'shanter) or several other characters used in addresses
  • It's limiting the address entry to 80 characters, which seems arbitrary and possibly too short

Generally speaking, I wouldn't validate addresses, unless you actually want to do a post-office (or delivery company) lookup to validate it's a real, actual address, and even then it's fraught with problems (eg, new address that isn't in the database yet, or it includes a Suite # that is not in the database).

Payment processors will also let you validate addresses against the credit card numbers, mostly as a form of fraud protection.

What's the point of validation? If you're shipping something, the onus is on the customer (who is presumably both paying and entering this address) to validate the correct address. Until you know why it needs to be valid, and exactly what is "valid", you can't use validation.

You can maybe require non-blank, or a minimum # of characters, but even that may get you into trouble with international users (depending on scope of this app).

于 2013-04-15T16:55:19.720 回答