1

I am working on a project that already existed when I arrived at the company.

Users need to paste several ISBN numbers from Excel to a search field in my Index page (HTML). They do so by selecting many ISBNs from Excel and copy/paste into the search field. The problem is that sometimes ISBNs are poorly formatted and may have a letter or special symbol in the middle (don’t ask me why, no idea).

The string resembles to something like this:

3356565663541 3356565663541 3356565663541 3356B565663541 3356565663541A

The code in my index page for the input form is:

<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 need a way to analyze what is pasted into the text box and signal to the user that something is wrong.

I'd like to do that in C# if possible but I have no idea on how to:

  • Detect the "paste into the textbox" with the ctr-v or right click, past;

  • Get the string to analyze it;

  • Show it on a new temporary window with highlighted color on the line that may contain an error.

Users would press OK if everything is correct or detect the line that is NOK.

ekad
  • 14,436
  • 26
  • 44
  • 46
Rui Ruivo
  • 343
  • 3
  • 12
  • Sounds like you want to use client-side detection, I suggest you to read this example first: https://stackoverflow.com/questions/2903991/how-to-detect-ctrlv-ctrlc-using-javascript. Note that the server-side code doesn't know if the text contained inside textbox is a result of copying and pasting verbatim. – Tetsuya Yamamoto Dec 12 '18 at 09:29

1 Answers1

1

Use the input event handler. It will trigger on typing and on pasting both

jQuery:

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

plain JS:

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

function isValidISBN (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 chars = isbn.split('');
  if(chars[9].toUpperCase() == 'X'){
    chars[9] = 10;
  }
  var sum = 0;
  for (var i = 0; i < chars.length; i++) {
    sum += ((10-i) * parseInt(chars[i]));
  };
  return ((sum % 11) == 0);
}

document.getElementById("ISBN").addEventListener("input",function() {
  this.value.split(" ").forEach(function(val) {
    if(val.trim()) console.log(val,isValidISBN(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 insist on having the server process it, do this (here jQuery):

$("#ISBN").on("input",function() {
  $.post("yourcsharpprocess",{ data : this.value },function(data) {
    $("#result").html(data);
  });
});
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • I don't want a standard code, i need to send the string into C# and to load it into a window. How can i call a C# method from HTML and send it the string? Thank you. – Rui Ruivo Dec 12 '18 at 09:58
  • 1
    @RuiRuivo: What you want to achieve according to **your question** is exactly what this answer tells you. You don't need to send it to the server, just to validate if an ISBN has a valid syntax (it even validates the checksum letter, which should catch most of the errors where two numbers are swapped) – Tseng Dec 12 '18 at 10:04
  • Yeah, why wait for a round trip - C# cannot detect a paste - I have updated the answer – mplungjan Dec 12 '18 at 10:09
  • OK i am trying. – Rui Ruivo Dec 12 '18 at 10:43