What is the best method for identifying empty spaces in a textbox field?

I am having an issue with the textbox keypress function. If the textbox field is empty, I do not want to post any values.

My current functions are working fine. When the textbox field is empty and I press enter key, it goes to the next line as expected. However, if I press the enter key two times, the values are posted.

Can someone please help me identify the problem in my code?


$(".ppop-comment").keypress(function(e) {
    if($('#add_comment').val()=="") {
        if (e.which == 32) 
            return false;
    } else if (e.keyCode == 13 && !e.shiftKey && !$('#add_comment').val()==" ") {
        $("#submit-comment").click(); 
    }
});
    
<form id="commentform" method="post">
    <textarea id="add_comment" name="meetnewpeople[message]" class="popup-comment"></textarea>
    <input id="submit-comment" type="button" value="Post a comment" />
</form>

Answer №1

 $("#comment-box").on("keypress", function(event) {
    if ($('#comment-input').val().trim() != "") {
        if (event.keyCode == 13 && !event.shiftKey && !$('#comment-input').val() == " ") {
            $("#submit-btn").click();
        }
    } else {
        return false;
    }
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Choose an option from a dropdown menu using JavaScript

Currently, I am working on a project that involves using javascrypt in conjunction with selenium. While attempting to search for and select an item from a list, I have encountered difficulties in selecting the element even after successfully locating it. I ...

Issues with jQuery compatibility when used alongside HTML and CSS

My coding tool is flagging errors in my JavaScript file, including: -'$' usage before definition. -Incorrect spacing between 'function' and '(' This is the content of my JS file: $(document).ready(function() { $(window).s ...

Stop all child components in React from mounting when the page loads

I am currently working on a single-page React application where I need to mount specific components at different times. However, all the components are loading simultaneously instead of one at a time. In my research on StackOverflow, I came across solutio ...

I have received a JSON multi-line string after entering information in a textarea

I've been working on a small social network project where users can create and share posts. However, I encountered an issue with formatting when storing and displaying the posts. When a user enters text with line breaks in the post creation form, it ...

Error in Asp.net: 'test' is not defined in Microsoft JScript runtime

I'm encountering an issue with an ImageButton that has an onclientclick() method. Whenever I click the button, I receive the error Microsoft JScript runtime error: 'test' is undefined. Below is the full code snippet: <%@ Page Langua ...

Passing variables with AngularJS and Typescript using a service

Currently in the process of developing an application using AngularJS, I am faced with the challenge of passing a URL when clicking on a menu button in order to utilize that URL within an iframe on another view controlled by a separate controller. Despite ...

Unchecking an available item observable in Knockout.js when clicking on a selected item

When you click on the elements in the top list, a selection is made. If you click on the elements in the bottom list, it will be removed from the list. However, the checkbox in the top list is not being unchecked. How can this issue be resolved? functio ...

Synchronizing validation processes across both front-end and back-end applications

How do you ensure validations remain synchronized between front-end and back-end teams when working with laravel? While I found some information in this post, I am curious about how it specifically applies to laravel. I came across the laravel js validat ...

Behat automates the process of populating form fields that are dynamically displayed as 'visible'

I'm facing an issue with a form that has hidden fields. When a user selects a checkbox, some of the hidden form fields are supposed to be revealed using the jQuery function slideToggle(). The code itself is pretty simple and you can see an example her ...

Dynamic key flow type where all keys are of the same type

My Firebase database is organized in the following manner: user: { cart: { "randomid": quantity, "randomid": quantity } } I want to avoid hardcoding item ids into a flow type. Is there a way to define a type where every key is a number, simil ...

Can you explain the purpose of using the 'apply' method in this particular implementation of memoization in JavaScript?

_.memoize = function(func) { var cache = []; return function(n){ if(cache[n]){ return cache[n]; } cache[n] = func.apply(this,arguments); return cache[n]; } }; I'm curious about the usage of 'this' in func.appl ...

Placing options and a clickable element within a collapsible navigation bar

Within my angular project, there are 4 input fields where users need to enter information, along with a button labeled Set All which will populate them. https://i.sstatic.net/1GGh1.png I am wondering how I can organize these input fields and the button i ...

Modifying hidden field values with JavaScript does not carry over to the server side in Chrome

I created a custom user control that contains a hidden field which is set when a node is clicked on a Tree View Hierarchy control. The following function is responsible for handling the click event of the Tree View: function HandleTreeClick(evt) { va ...

Store the information from an AJAX post request in a div element into the browser

I've set up a datatable with a button that posts data into a div. Below is the HTML code I used: HTML : <div class="card-body"> <div class="overlay" id="kt_datatable_nodata"> <div class="o ...

Adjust the size of an element in response to changes in the size of

I've implemented a jQuery function to resize an element dynamically based on window size changes: $(window).resize(function() { topHeight = $("#top").height(); height = $(window).height() - 210; $("#container").height(height); $("#g").height( ...

Sending JSON Data over URL

I am facing a challenge with passing data between two HTML files. My initial solution involved sending the data through the URL using the hash and then parsing the link using JSON.parse(window.location.hash.slice(1));. This method worked for a few attempts ...

Having difficulty erasing the existing input

I'm trying to create a form input field in JavaScript that generates a specified number of additional inputs based on user input. The issue I'm facing is that while the code works and adds more input fields, it does not delete the previously gene ...

The mssql node is experiencing an issue where it is unable to accept work due to the pool being

Recently, I encountered an issue with my node js app that utilizes the npm mssql module. Despite purchasing a cloud Windows 2012 server, I faced an error when trying to execute a stored procedure. The error is thrown at ps.prepare("exec usp_Get_Cars @para ...

Is it possible to use ref in React to reference various elements based on specific actions?

I'm having trouble targeting the clicked button using a ref as I always get the second one. Any ideas on how to solve this issue? Also, if I have a native select element with two optgroups, is it possible to determine from which optgroup the selection ...

"What could be the reason why the color property is not being applied to this

I have been attempting to change the color of an icon by using the color property, but for some reason, it is not applying as expected. Here is the code snippet I am working with: "& .MuiListItemIcon-root": { color: "red" }, ...