2014-04-23 27 views
0

我有以下代碼,它在限制字符和顯示警報時適用於我,但我試圖限制而不是字符。任何人都可以幫忙嗎?謝謝!帶警報的文本區域限制字

<script type="text/javascript"> 

$(function() { 
    $('a[href^="#"]').click(function (e) { 
     if ($("#promotion_image_description").val().length < 1) { 
      alert("You must enter your story!"); 
      return false; 
     } 

     if ($("#promotion_image_description").val().length > 1300) { 
      alert("Your story can not be longer than 250 words!"); 
      return false; 

     } else { 
      switch ($(this).attr('href')) { 
       case '#promotion': 
        $('#promotion_facebook_id_block, #promotion_name_block, #promotion_email_block, #promotion_address_1_block, #promotion_csz_block, #promotion_country_block, #promotion_phone_block, #promotion_custom_field_9_block, #promotion_custom_field_8_block, #promotion_custom_field_4_block, #promotion_custom_field_2_block, #promotion_custom_field_5_block, #promotion_custom_field_6_block, #promotion_custom_field_3_block, #promotion_agree_block, #promotion_submit_block, #disclaimer_promotion').show(); 
        $('#promotion_custom_field_10_block, #promotion_image_description_block, #promotion_custom_field_11_block').hide(); 
        e.preventDefault(); 
        break; 
      } 
     } 
    }); 

}); 

</script> 
+2

這裏已經討論http://stackoverflow.com/questions/1469304/limit-words-in-a-textarea –

+0

@prabhakar是的,但有10個答案,我不知道哪些工作在我的某些代碼。 – Zach

回答

1

相反的:

if ($("#promotion_image_description").val().length > 1300) { 

務必:

if ($("#promotion_image_description").val().match(/\S+/g).length > 250) { 
+0

這工作完美!謝謝你,先生! – Zach