Identifying copy and paste behavior within an HTML form using .NET CORE MVC 2.1

I inherited a project at my current company and one of the tasks involves allowing users to paste multiple ISBN numbers from Excel into a search field on my Index page (HTML). Sometimes, the ISBNs are poorly formatted with letters or special symbols mixed in for some unknown reason.

The input might look something like this:

3356565663541 3356565663541 3356565663541 3356B565663541 3356565663541A

Currently, the code on my index page for the input form is as follows:

<div class="col-lg-4">
  <div class="form-group>
    <label>ISBN</label>
    <input asp-for="Advanced_ISBN" id="ISBN" type="text" placeholder="" class="form-control">
  </div>
</div>

I now need to find a way to analyze the pasted text in the textbox and alert the user if there are any errors. I would prefer to do this using C#, but I'm not sure how to proceed.

  • Detect when the user pastes text into the textbox using ctrl-v or right-click and paste;

  • Retrieve the pasted string to analyze it;

  • Show the analyzed text in a new temporary window highlighting any lines that may contain errors.

Once presented with the highlighted text, users can either confirm everything is correct by pressing OK or identify and address any lines that are incorrect.

Answer №1

Utilize the input event handler to trigger actions on typing and pasting.

jQuery:

$("ISBN").on("input",function() {}); 

plain JS:

document.getElementById("ISBN").addEventListener("input",function() {}); 

function validateISBN (isbn) { // from https://neilang.com/articles/how-to-check-if-an-isbn-is-valid-in-javascript/
  isbn = isbn.replace(/[^\dX]/gi, '');
  if(isbn.length != 10){
    return false;
  }
  var characters = isbn.split('');
  if(characters[9].toUpperCase() == 'X'){
    characters[9] = 10;
  }
  var sum = 0;
  for (var i = 0; i < characters.length; i++) {
    sum += ((10-i) * parseInt(characters[i]));
  };
  return ((sum % 11) == 0);
}

document.getElementById("ISBN").addEventListener("input",function() {
  this.value.split(" ").forEach(function(val) {
    if(val.trim()) console.log(val,validateISBN(val));
  });
});
A valid ISBN would be 0-85131-041-9

<div class="col-lg-4">
  <div class="form-group">
    <label>ISBN</label>
    <input asp-for="Advanced_ISBN" id="ISBN" type="text" placeholder="" class="form-control">
  </div>
</div>

If you prefer processing it server-side with jQuery, follow these steps:

$("#ISBN").on("input",function() {
  $.post("yourcsharpprocess",{ data : this.value },function(data) {
    $("#result").html(data);
  });
});

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

What is the best way to ensure jQuery runs as soon as the page loads?

In my current jQuery script, I have implemented a feature that checks for content in two input fields (a login page with username and password) and adds the class "used" if there is content present. $(window, document, undefined).ready(function() { $(&a ...

Is it possible to change the inner html to null during an active ajax request?

My goal is to have two separate data.html files inserted into two different HTML files without needing a click event. I want the structure of my data.html files to remain consistent, while allowing the template of my website to change dynamically by callin ...

Using tabs within a Dialog prevents the Dialog from adjusting its height automatically

Solved: Find the solution in the comments section below. I recently built a Tabs feature within a Dialog box, but ran into an issue where the height of the Dialog did not match up with the height of the tab. The problem arose when a form was placed insid ...

dirpagination fails to display all rows in the dataset

I've been working on creating tables with 3 divs, as shown in the link below:- https://github.com/anirbanmishra/congress.php/blob/master/web_test Additionally, I have a javascript file available here:- https://github.com/anirbanmishra/congress.php/bl ...

Tips for refreshing retrieved data from the server upon page creation in Vue.JS

Upon page load, data is retrieved from the server and displayed in a table format like the one shown below: <tr v-for="option in options"> <td> < ...

Exploring the power of AngularJS through nested directives

Index.html: <nav-wrapper title="Email Test"> <nav-elem value="first"></nav-elem> <nav-elem value="second"></nav-elem> </nav-wrapper> app.js: app.directive('navWrapper', function() { return { ...

Clicking to remove added child element

I have written a script that creates an image popup when the site loads. Here is the code: <script type="text/javascript"> function showPopup() { var div = document.createElement('div'); div.className += 'popup'; div.inne ...

Adding dynamic elements to a pop-up window with the use of jQuery's .load() function

When implementing Javascript to create a modal, I have encountered an issue where the close button disappears. The modal opens and loads different HTML files dynamically using the jQuery load() method, but the close button vanishes. I suspect that the mod ...

What types of events can be used to trigger the start of HTML5 audio playback in mobile Chrome?

When it comes to mobile browsers, Audio elements require user action before play can start. The click event usually satisfies this requirement, but touchstart doesn't seem to be an acceptable initiating event in Chrome on Android or iOS. (Refer below ...

Enhance your browsing experience by inputting valuable information into the

I am looking to enhance a text box by adding values. The text box already has a default value of 10, and I want to create a button that will add new text boxes with a limit of 4. My goal is to continuously add values from text box 1 to text box 4. For exa ...

Persistent pseudo-element problem across all iterations of Internet Explorer

Encountering an issue in IE 9, 10, 11, and Edge involving pseudo elements with transparent backgrounds. The problem arises when the repeating background image appears darker in the first half compared to the second half, giving the impression of overlap be ...

Guide to create a sliding menu transition from the viewport to the header

I am just starting to learn jQuery and I want to create a menu similar to the one on this website Specifically, I would like the menu to slide down from the viewport to the header, as shown in the template in the link. I have searched through the jQuery ...

Display HTML element within the <samp> tag using PHP

I'm using Bootstrap for my form control, but my input and button are not on the same line. <div class="form-group"> <label class="col-lg-3 control-label" id="title-meaning">Meaning:</label> <div c ...

Sharing a codepen.io project on a GitHub page

After completing the courses on free code camp, I successfully created a tribute page project on codepen.io. However, when attempting to display the finished website on GitHub using GitHub pages, I have encountered issues. No matter what steps I take, the ...

Setting the parent's height to match one of its children

I'm struggling to align the height of the pink '#images-wrap' with the main image. When there are too many small rollover images on the right, it causes the pink div to extend beyond the main image's height. If I could make them match i ...

I'm having trouble with Material Design Slide Toggle as it lacks event.StopPropagation functionality. Any suggestions on what alternative I

When working with the Slide Toggle in Material Design, I noticed that it does not have a stopPropagation event. This is because the "MdSlideToggle.prototype._onChangeEvent" already includes a call to stopPropagation. So, what should be used instead? <m ...

Redirect the Submit button to the specified URL from the form element

Looking for a way to create a form with two fields: Username and Password. Upon clicking submit, the username and password should be added to the URL in a specific format. For instance: The URL structure will remain the same, only the "username_entered_i ...

What is the main purpose of using the CSS transform:translate() property?

When CSS3 introduced animations with transition properties, it gave developers two main ways to change the position of an element. The first method involves setting the element's position as absolute and adjusting the left, right, top, and bottom prop ...

Showing local storage on a webpage using Jquery

I have encountered an issue where I can successfully add and remove items from local storage, but the added item does not display on my HTML page. The expected behavior is that when an item is added to local storage, it should be visible on a favorites pag ...

What is the most efficient way to send various props to multiple child components within a React parent component?

Currently, I am working on developing a color palette generator using React. However, I have encountered an issue where all child divs in the color palette receive the same color instead of different colors. Below is the code snippet: class ColorPalet ...