通过jquery验证表单时,确保字段至少包含一个字母

发布于 2024-09-14 03:18:30 字数 481 浏览 1 评论 0原文

所以,我非常不擅长正则表达式和 JavaScript。因此,当我意识到我需要通过 jquery 验证表单字段以确保至少使用一个字母字符时,我决定向你们寻求一些帮助。

我想允许用户在此字段中使用撇号、空格、破折号、句点、下划线和字母数字字符。但是,它们必须至少使用一个字母字符。

这是我创建的验证器方法,它可以执行所有操作,但确保至少使用一个 alpha 字符。

 $.validator.addMethod("nameRegex", function(value, element) {
        return this.optional(element) || /[^a-z/g]+/gi.test(value) || !/[^\.\'\:\-_ a-z0-9]+/gi.test(value);
    }, "Name must contain at least one letter.");

有什么建议/提示/见解/侮辱吗?谢谢大家!

So, I suck terribly at regex and javascript. As such, when I realized I needed to validate a form field via jquery to ensure that at least one alpha character was used, I decided to ask you fine folks for some help.

I want to allow user to use apostrophes, spaces, dashes, periods, underscores, and alphanumeric chars in this field. They must, however, use at least one alpha character.

Here is the validator method I created, which does everything but ensure that at least one alpha char is used.

 $.validator.addMethod("nameRegex", function(value, element) {
        return this.optional(element) || /[^a-z/g]+/gi.test(value) || !/[^\.\'\:\-_ a-z0-9]+/gi.test(value);
    }, "Name must contain at least one letter.");

Any tips/hints/insights/insults? Thanks everyone!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

躲猫猫 2024-09-21 03:18:30

我很幸运地提前查找了所需的字母,然后仅匹配最小长度的有效字符。在我的示例中,我使用的最小长度为 8 个字符,有效字符包括撇号、空格、连字符、句号、下划线和字母数字字符。我相信 '\w' 字符类应该匹配下划线和字母数字。它似乎在我的测试中有效:

var r = new RegExp("^(?=([` -.]|\\w)*?[a-zA-Z])([` -.]|\\w){8,}$")
> r.test('12345678') //all numbers shouldn't work
false
> r.test('12345678a') //long enough and one letter should work
true
> r.test('12345`_bc') //allow ` _
true
> r.test('a12345`_') //shouldn't matter if letter is in front
true
> r.test('123a45`_') //shouldn't matter if letter is in middle
true
> r.test('12345`_29') //even if special characters no letter should not work
false

编辑:我添加了一个“?”在前瞻中的“*”之后使量词变得非贪婪。另外我应该指出,我在 Chrome 中对此进行了测试,显然我的测试用例并不是结论性的。我希望这会有所帮助。另一种方法是匹配零到多个有效字符,至少 1 个字母,然后匹配零到多个有效字符。像这样的东西:

var r = new RegExp("^([` -.]|\\w)*[a-zA-Z]+([` -.]|\\w)*$")

I have had luck doing a look ahead for the required letter then matching only on valid characters for the minimum length. In my example I use a minimum length of 8 characters with valid characters of apostrophe, space, hyphen, period, underscore, and alphanumeric characters. The '\w' character class is suppose to match underscore and alphanumerics I believe. It seems to work in my tests:

var r = new RegExp("^(?=([` -.]|\\w)*?[a-zA-Z])([` -.]|\\w){8,}$")
> r.test('12345678') //all numbers shouldn't work
false
> r.test('12345678a') //long enough and one letter should work
true
> r.test('12345`_bc') //allow ` _
true
> r.test('a12345`_') //shouldn't matter if letter is in front
true
> r.test('123a45`_') //shouldn't matter if letter is in middle
true
> r.test('12345`_29') //even if special characters no letter should not work
false

EDIT: I added a '?' after the '*' in the look ahead to make the quantifier non-greedy. Also I should note that I tested this in Chrome and obviously my test cases are not conclusive. I hope this helps though. Another approach is to match zero to many valid characters, at least 1 letter, then zero to many valid characters. Something like:

var r = new RegExp("^([` -.]|\\w)*[a-zA-Z]+([` -.]|\\w)*$")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文