2013-06-06 24 views
0

我想弄清楚如何讓我的jQuery驗證工作,以便如果一個值小於所需的數量將顯示錯誤代碼。我無法弄清楚如何使這個工作正確。它至少需要一個字符輸入,但我希望它至少爲4jquery表單驗證如何要求最小輸入

任何幫助表示讚賞

我的驗證碼看起來像

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

回答

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

感謝您的幫助 – hmmm

1

您比較的實際值字符串,而不是我假設你想要的長度。只需添加一個.length到你的條件。像這樣:

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

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"]