简体   繁体   English

需要 javascript 表单验证器的帮助

[英]Require help with javascript form validator

i am relatively new to javascript and have to design a form vlalidator with the following fields我对 javascript 比较陌生,必须设计一个具有以下字段的表单验证器

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 use regex c or what?我应该使用regex c 还是什么?

For capital letter this holds: str[0] >= 'A' && str[0] <= 'Z' .对于大写字母,这成立: str[0] >= 'A' && str[0] <= 'Z'

Only letters: a regexp like /^[az]$/i should match.只有字母:像/^[az]$/i这样的正则表达式应该匹配。

Length: str.length >= 10 .长度: str.length >= 10

Symbols: regexp like str.replace(/[^@#$%^&]+/, "").length >= 2 .符号:像str.replace(/[^@#$%^&]+/, "").length >= 2这样的正则表达式。

If you want to only use regexps for the password, you could do:如果您只想使用正则表达式作为密码,您可以这样做:

/[@#$%^&]{2,}/.test(str) && /.{10,}/.test(str)

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.它只需要某种翻译方法 - 或者只是用您的 output 文本替换调用。

in the html you set classes for each validator-specification like capitalletter:在 html 中,您为每个验证器规范设置类,例如大写字母:

<input class="capitalletter" type="text" name="somename" />

and then implement the function capitalletter in the validator object...然后在验证器 object 中实现 function 大写字母...

JS call will be: JS 调用将是:

var my_validator = new validator(yourformID);
my_validator.validate()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM