0

我试图弄清楚如何让我的 jquery 验证工作,以便如果一个值小于所需的数量,它将显示错误代码。我不知道如何使它正常工作。它需要至少输入一个字符,但我希望它至少为 4 个。

任何帮助表示赞赏

我的验证码看起来像

   if($('#firstname').val() < 3){
                $('#firstname').parent().parent().find('.form-error').html("First Name is Required");
                err++;
            }
4

3 回答 3

1

您正在比较字符串的实际值,而不是像我假设的那样比较长度。只需为您的条件添加一个 .length 即可。像这样:

if($('#firstname').val().length < 4)

于 2013-06-06T04:23:25.947 回答
0

.length可能会简单地解决您的问题。
但我建议使用正则表达式来检查输入,否则用户可以只输入 4 个空格,这也将通过.length检查。

str = "    " # 4 space
str.length >= 4 # => true
str.match(/^\w{4}.*$/) # => null
another_str = "someinput"
another_str.match(/^\w{4}.*$/) # => ["someinput"]
于 2013-06-06T04:47:46.247 回答
0
if($('#firstname').val().length < 4){
    ...
于 2013-06-06T04:24:37.467 回答