需要有关 javascript 表单验证器的帮助
我对 javascript 比较陌生,必须设计一个包含以下字段的表单 vlalidator
name must start with a capital letter and must have only the characters a-z and A-Z and password_field => (must contain atleast two symbols from the list [ @,#,$,%,^,& ]
满足上述所有条件的最佳方法是什么?我应该使用regex
c 还是什么?
i am relatively new to javascript and have to design a form vlalidator with the following fields
name must start with a capital letter and must have only the characters a-z and A-Z and password_field => (must contain atleast two symbols from the list [ @,#,$,%,^,& ]
whats the best way to meet all the above conditions? should i useregex
c or what?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正则表达式肯定会有帮助。
这是一个验证器的示例,很容易自定义: validator
它只需要某种翻译方法 - 或者只是用输出文本替换调用。
在 html 中,您为每个验证器规范设置类,例如
大写字母:
,然后在验证器对象...
JS 调用将是:
regex will definitively be helpful.
here is an axample of a validator, which is easy to customize: validator
it just requires some kind of translate method - or just replace the calls with your output text.
in the html you set classes for each validator-specification like capitalletter:
<input class="capitalletter" type="text" name="somename" />
and then implement the function capitalletter in the validator object...
JS call will be:
对于大写字母,这成立:
str[0] >= 'A' && str[0] <= 'Z'
。仅字母:像
/^[az]$/i
这样的正则表达式应该匹配。长度:
str.length >= 10
。符号:正则表达式,如
str.replace(/[^@#$%^&]+/, "").length >= 2
。如果您只想使用正则表达式作为密码,您可以这样做:
For capital letter this holds:
str[0] >= 'A' && str[0] <= 'Z'
.Only letters: a regexp like
/^[a-z]$/i
should match.Length:
str.length >= 10
.Symbols: regexp like
str.replace(/[^@#$%^&]+/, "").length >= 2
.If you want to only use regexps for the password, you could do: