用警报限制textarea中的单词

时间:2014-04-23 00:31:18

标签: javascript jquery html

我有以下代码,在限制字符和显示提醒时对我有用,但我试图限制而不是字符。有人能提供帮助吗?谢谢!

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

1 个答案:

答案 0 :(得分:1)

而不是:

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

执行:

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