Homepage Forums Dev & Debugging What’s going wrong? I am looking for form validation onkeyup

  • What’s going wrong? I am looking for form validation onkeyup

     satyamukherjee updated 10 months ago 4 Members · 4 Posts
  • douglas.virginia

    Member
    December 23, 2020 at 10:06 am
    20 Coins
    0 Gems
    Level: Rookie

    I am using this code to send value when user hits enter. What I want is if user hits enter without entering any value, he gets an error:

    <textarea class="auto-grow-input" onfocus="SK_focusChat();" onkeyup="SK_sendChatMessage(this.value,<?php echo $sk['chat']['recipient']['id']; ?>,event);"></textarea>

    #Send message

    function SK_sendChatMessage(text,recipient_id,e) {

    document.title = document_title;

    textarea_wrapper = $('.chat-textarea');

    chat_messages_wrapper = $('.chat-messages');

    if (e.keyCode == 13 && e.shiftKey == 0) {

    e.preventDefault();

    textarea_wrapper.find('textarea').val('');

    chat_messages_wrapper.append('<div class="chat-text align-right temp-text" align="right"><div class="text-wrapper float-right">' + text + '<div class="marker-out"><div class="marker-in"></div></div></div><div class="float-clear"></div></div>');

    $.post(SK_source() + '?t=chat&a=send_message', {text: text, recipient_id: recipient_id}, function (data) {

    chat_messages_wrapper

    .append(data.html)

    .scrollTop(chat_messages_wrapper.prop('scrollHeight'))

    .find('.temp-text')

    .remove();

    });

    }

    }

  • Avery Walter

    Member
    December 23, 2020 at 12:44 pm
    12 Coins
    0 Gems
    Level: Rookie
    Helpful
    Up
    0
    Down
    Not Helpful

    JQuery form validation will be the way to go.

    <style>
    .error { color: red; }
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
    <script>
    $(document).ready(function() {
    // validate signup form on keyup and submit
    $("#omfg").validate({
    rules: {
    omfgdood: "required"
    },
    messages: {
    omfgdood: "Oy! It\'s Empty!"
    }
    });
    });
    </script>
    <?php $sk['chat']['recipient']['id'] = 'omfgdood'; ?>
    <form id="omfg">
    <textarea class="auto-grow-input" id="<?php echo $sk['chat']['recipient']['id']; ?>" name="<?php echo $sk['chat']['recipient']['id']; ?>"></textarea>
    <input type="submit" name="submit" value="Submit" />
    </form>
  • Abhik Biswas

    Member
    December 23, 2020 at 12:47 pm
    125 Coins
    0 Gems
    Helpful
    Up
    0
    Down
    Not Helpful

    You can do something like this…

    if ($('textarea.auto-grow-input').text().length === 0) {
    alert('Must enter text');
    return false;
    }
  • satyamukherjee

    Administrator
    December 23, 2020 at 12:54 pm
    11500 Coins
    567 Gems
    Helpful
    Up
    0
    Down
    Not Helpful

    You can also write your SK_sendChatMessage function like

    function SK_sendChatMessage(text,recipient_id,e) {

    document.title = document_title;

    textarea_wrapper = $('.chat-textarea');

    chat_messages_wrapper = $('.chat-messages');

    if (e.keyCode == 13 && e.shiftKey == 0 && text == '') {

    alert('enter text');

    }

    else{

    textarea_wrapper.find('textarea').val('');

    chat_messages_wrapper.append('<div class="chat-text align-right temp-text" align="right"><div class="text-wrapper float-right">' + text + '<div class="marker-out"><div class="marker-in"></div></div></div><div class="float-clear"></div></div>');

    $.post(SK_source() + '?t=chat&a=send_message', {text: text, recipient_id: recipient_id}, function (data) {

    chat_messages_wrapper

    .append(data.html)

    .scrollTop(chat_messages_wrapper.prop('scrollHeight'))

    .find('.temp-text')

    .remove();

    });

    }

Log in to reply.

Original Post
0 of 0 posts June 2018
Now

New Report

Close