数字逗号和空格的正则表达式 [英] regex for numbers comma and space
本文介绍了数字逗号和空格的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文本区域,用户可以在其中输入以逗号或(逗号和空格)分隔的美国邮政编码.
I have a text area and a user can input US zip codes in it separated by a comma or (comma and space).
它可能像12345、45678、89654
It could be like 12345,45678, 89654
以下正则表达式正在运行并且正在删除不允许的字符:
The following regex is working and is removing not allowed characters:
$object.val($object.val().replace(/[^\d\, ]/g, ''));
我想增强它,以便
- 我总是应该在开头有 5 位数字
- 5 位数字后应有逗号或逗号和空格
- 逗号或(逗号和空格)不应位于字符串的最末尾.末尾必须是 5 位数字.
这也需要解决复制粘贴问题.用户可以复制粘贴无效长度的邮政编码.
This needs to tackle the copy paste as well. The user may copy paste invalid length for the zip code.
谢谢
推荐答案
使用这个正则表达式:^\d{5}(, ?\d{5})*$
开头指定5位数字:^\d{5}
以及任意数量的其他逗号、空格和 5 位数字组合:(, ?\d{5})*
It specifies 5 digits at the beginning: ^\d{5}
and any number of other comma, space, and 5 digit combinations: (, ?\d{5})*
这篇关于数字逗号和空格的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文