0

I have the following code which works just fine for me when limiting characters and showing an alert but I am trying to limit words instead of characters. Anybody able to help? Thanks!

<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>
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
Zach
  • 3
  • 1

1 Answers1

1

Instead of:

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

Do:

if ($("#promotion_image_description").val().match(/\S+/g).length > 250 ) {
Jack
  • 20,735
  • 11
  • 48
  • 48