Check for empty input and add anti flooding function?

$('#submsgp').click(function(){

        ping_waiting=1;

        $.ajax({
            type: 'POST',
            data: 'op=svm&room_id=0&id_faq=&cplugin=&ss=hhsit&last_id='+$('#last_id').val()+'&ac=savm&type=chat&cont='+$('#msgchatter').val(),
            url: 'http://localhost/test/hhsit?op=svm',
            dataType: 'json',
            crossDomain : true,
            success : function (response) {




                if (response.quest!='ok' && response.status!=undefined){
                    if (response.lastid!=undefined && response.status_text!=''  && $('#last_id').val(response.lastid)!=response.lastid){
                        $('#chat_bodyCtr').append(response.status_text);
                    }
                }

                if (response.status_text!='' && response.status_text!=undefined){
                    if (response.lastid!=undefined && response.status_text!=''  && $('#last_id').val(response.lastid)!=response.lastid){
                        $('.nofound_message').hide();
//                        chatter_play_s('incoming');
                    }
                }

                if (response.lastid!=undefined && response.status_text!=''  && $('#last_id').val(response.lastid)!=response.lastid){
                    var d = $('.chat_bodyCtr');
                    d.animate({ scrollTop: d.prop('scrollHeight') }, 1000);
                }


                if (response.lastid!=undefined && response.lastid>0){
                    $('#last_id').val(response.lastid);
                }
            },
            error:function(jXHR, textStatus, errorThrown) { 
                alert('Error while sending message: '+errorThrown+'...');
                ping_waiting=0;
            },
            complete: function() {
                $('#msgchatter').attr('value', '');
                ping_waiting=0;

            },
            timeout: 5000,
            cache: false
        });

        return false;
    });

    var d = $('#chat_bodyCtr');
    d.animate({ scrollTop: d.prop('scrollHeight')+500 }, 1000);


    setInterval('chatter_doAjaxPing()',timex_pingx);

})({});

Guys I am terrible with jQuery.

Could you please help me out to somehow prevent users from posting messages in less than 5 seconds apart.

And don’t post message that only contains spaces. Needs to contain at least 2 real letters or numbers.

To prevent users from posting frequently, you’ll need a backend database to track a unique id in relation to that user, so that the backend can check if that user has already posted before within the past minute, and deny them.

To prevent short messages, you need to validate the form field before it’s submitted. That isn’t done with the code that you have there because it’s retrieving information instead.

To ignore leading and trailing spaces, you can adjust that using the trim method.
As you’re using jQuery, you can use the jQuery.trim() method instead.

When submitting the form, you would also want to check after trimming the message, that the message length is greater than 2 before allowing it to be submitted.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.