Disabling paste functionality in a textfield can be achieved by adding a

I need to create two textfields, one for entering an email and the other for confirming the email. I want to prevent the user from pasting anything into the confirm textfield using ctrl+v or the mouse in a Struts 2 application.

Most examples I've come across only use simple HTML for this functionality. Is it possible to achieve this using a textfield in Struts 2?

Answer №1

One issue with the previous examples is that they all relied on the "onPaste" event in the HTML edit text, which the textfield of Struts2 does not have.

This actually isn't a problem for Struts UI Tags that have:

Dynamic Attributes Allowed: true

<s:textfield/>, as well as <s:file/>, <s:textarea/>, and others, support it, as detailed in the documentation (under Parameters).

What this means is that anything you input in the tag (that isn't a Struts UI Tag attribute) will be included in the generated HTML as-is, allowing you to utilize new functionalities without necessitating Apache to roll out new Tag versions.

Answer №2

After some troubleshooting, I managed to come up with a solution in my JSP file:

<s:textfield name="confermaEmail" id="idConfermaEmail" size="30"  onFocus="disablePaste()"/>

To achieve this, I implemented a JavaScript function:

function disablePaste(){
    var input = document.getElementById("idConfermaEmail");
    if (input)
       input.onpaste = function(){return false;};
}

I tested this code on Internet Explorer 10 and Chrome browsers.

Additionally, as mentioned by @Andrea Ligios in another comment, it is also possible to utilize the onPaste event in Struts 2.

Edit: Upon further evaluation, using onFocus instead of onPaste proves to be more effective. The onPaste event only blocks pasting after the first instance, whereas onFocus prevents pasting immediately upon focus, before any paste occurs.

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

Encountering a problem with Node.js because of a SyntaxError: Receiving an

Recently, I decided to dive into learning node.js and attempted something very basic: displaying a "Hello World" message from the server. The code snippet I used (taken directly from a book) is as follows: var http = require("http"); http.createServer(fu ...

Does this task require a high amount of CPU resources for Node.js on the back-end?

I am currently developing an Android app and I am faced with a decision on whether to utilize node.js or PHP for the back-end. The task at hand involves users inputting query parameters, such as zip codes, which are then used to perform database queries ...

Capturing link titles for control navigation in Nivo Slider

Essentially, in the nivoslider plugin, the controlnav section displays numbers based on the "rel" attribute in the code. I am wondering if there is a way to modify this so that it retrieves the "title" or "alt" from the link added to the slideshow. This wo ...

Explore jQuery UI Tabs with remote content, highlighting the active tab with a link to the current page

Back in earlier versions of jQuery UI (<=1.8), when loading tabs, the link for a remote tab would be replaced with a local link. This allowed users to middle-click (or open in new browser tab) on the tab, and it would link back to the current page with ...

Explore jQuery to locate a specific anchor element that has a matching URL and then apply a new class

I am looking to highlight both the parent and child items in a navigation menu by returning URLs as an array based on the current page. The structure of my basic navigation is as follows: <div id="cssmenu"> <div id="menu-button"></div&g ...

Updating the load context variable in Django template after making changes via an AJAX call: a step-by-step guide

Here is the code snippet for displaying table items in my customer_items.html page: <table id="tblData" style="width: 100% !important;" class="display table table-bordered table-striped table-condensed"> <thead> <tr> ...

React Forms: Dynamically Updating the User Interface using WithFormik() and Axios GET Response

I've been using withFormik() to create a form for my Gatsby application. Currently, I am implementing a GET request using axios within the handleSubmit() function of withFormik(). Once I receive the response, I want to update it on the UI immediately ...

When the jquery loop fails to function

I need to dynamically add a class to a specific tag using jQuery depending on an if condition. Here's the code snippet: if ($(".asp:contains('Home')")) { $("ul.nav-pills a:contains('Home')"). parent().addClass('active ...

Upon installation, the extension that replaces the new tab fails to detect the index.html file

edit: Check out the Chrome Extension here Edit 2: It seems that the recent update containing the index.html file was not published due to Google putting it under revision. Apologies for forgetting to include the index.html file in the upload zip, as I ...

Retrieve the property value from a nested object using a key that contains spaces

Presenting my object: let obj = { innerObj: { "Key with spaces": "Value you seek" } } Upon receiving, I am unaware of the content within obj. I possess a string variable holding the key to access the value. It appears as follows: let ke ...

PHP and Bootstrap combine in this dynamic carousel featuring thumbnail navigation

Looking to create a dynamic bootstrap carousel with image thumbnails at the bottom? After exploring various code snippets and solutions, I stumbled upon this link. While the code worked, I found the thumbnails to be too small. Below are the functions I use ...

Show loading GIF on the screen and hide it once the script has finished loading

I have implemented lazy loading for my Facebook comments plugin, but I am facing an issue where the plugin takes some time to load. To provide a better user experience, I want to display a loading GIF while the plugin is loading. However, once the comments ...

Is it possible to modify the sub/child divs within a draggable parent div and assign them a different class?

Greetings, after being a long-time reader, I have finally decided to post for the first time. In the process of creating a webpage with jQuery drag and drop divs, I am curious about how to change the class of a child div within a draggable div once it is d ...

Updating Mysql through REST API/JWT using PUT method is not possible

I have been attempting to send an update request using Jwt (Tokens) and Node.Js with a backend in mysql. While Postman confirms that the record has been successfully updated, I am unable to locate where the actual update occurred. No changes seem to be ref ...

Angular - Organized hierarchy drag-and-drop functionality

Currently, I am working on an Angular application where I aim to incorporate a nested drag and drop feature with the ability to sort items within their own list. You can find a helpful StackBlitz example demonstrating how to implement nested drag and drop ...

Error occurred while retrieving JSON array using Jquery

var groupMembers = $.parseJSON(members); $.each(groupMembers, function (index, item) { $.ajax({ type: "POST", url: "'.base_url(" / panel / listNotifications ").'/'.$id.'/" + valueSelected, d ...

Is there a way to condense my child table directly below its parent column?

When attempting to collapse the "child tr" within the "tr" in my code, the collapse is not positioning correctly underneath its parent tr. While it works fine in a JSFiddle, the issue arises when using my editor and live preview. In this scenario, both "ch ...

What is causing it to give me 'undefined' as the result?

Trying to execute a script, test.al(); and within test.al, it calls getcrypt.php();. The php script is hosted on a webserver, and it is functioning properly. Here are the current scripts being used: JS var getcrypt = { php: function () { $.a ...

PugJs - unable to locate file / file does not exist

Today marks my initial attempt at delving into pugJs and utilizing the command prompt, yet I encountered an error: Error: ENOENT: no such file or directory, lstat 'C:\Users\aya' Click here to view the screenshot of the error displa ...

Sveltekit - dynamic loader wheel for various loading sources with automatic retry feature

I want to create a simple spinner for my SvelteKit pages, but I'm unsure of the best approach. The goal is to display a loading spinner when the page loads a maplibre map. If the loading process fails, I need it to retry after 5 seconds. Even after th ...