字段验证需要正则表达式帮助
我需要按照以下要求验证该字段:
- 必须至少 6 个字符,
- 不得超过 50 个字符。
- 必须包含至少一个字母字符
- 必须至少有一个“其他”字符。 “其他”字符只能是数字或以下 8 个字符中的任何一个:下划线、连字符、句点、与号、美元、星号、感叹号、@ 符号
我想出了以下正则表达式,但它并不总是有效它允许一些我想排除的特殊字符
/^(?!.*(.)\1)((?=.*[^\w\d\s])(?=.*\w)|(?=.*[\d])(?=.*\w)).{6,50}$/
I need to validate the field with the following requirements:
- must be at least 6 characters
- must be no more than 50 characters.
- must contain at least one alphabetical character
- There must be at least one "Other" character.
An "Other" character can only be either a number or any one of the following 8 characters: underscore, hyphen, period, ampersand, dollar, star, exclamation, the @ symbol
I came up with the following regex but it does not always works it allows some special characters which I want to exclude
/^(?!.*(.)\1)((?=.*[^\w\d\s])(?=.*\w)|(?=.*[\d])(?=.*\w)).{6,50}$/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它真的必须是正则表达式吗?我只会编写一个函数来测试这些标准。
那不是更容易吗?
Does it really have to be a regexp? I would just write a function that tests each of these criteria.
Isn't that easier?
这是因为您在末尾匹配
.
,因此如果满足所有条件,则最多 50 个剩余字符可以是任何内容。我会用:It's because you match with
.
at the end, so if all your conditions are met, then any leftover characters up to 50 can be anything. I would use:我想说你需要以下内容:(
字母或符号)*
字母+
符号+
(字母或符号)*
OR
(字母或符号)*
符号+
字母+
(字母或符号)*
I would say you need the following:
(letters or symbols)*
letters+
symbols+
(letters or symbols)*
OR
(letters or symbols)*
symbols+
letters+
(letters or symbols)*