Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent a user from removing the first three characters in a text input?

I have a text input, which has three dynamically-generated characters On when page-load; what I want is that when a user enters data in that fieLd the user should be unable to remove that first three characters from the input.

The text input can contain 10 characters in total, three which we generate and 7 entered by the user.

Currently if the user presses backspace he can delete all characters, but I don't want to allow them to delete the first three characters.

On page-load the script sets three characters in the Lab text input:

<input id="Lab" maxlength="10" name="LabWareId" type="text" value="" class="valid">

$('#Lab').keyup(function () {
  if ($(this).val().length == $(this).attr('maxlength'))
    $('#DateReceived').focus();
});
like image 500
ZCoder Avatar asked Mar 30 '16 20:03

ZCoder


People also ask

How to remove first 3 characters in Excel?

Use the RIGHT Function to Remove First 3 Characters in Excel The combination of the RIGHT function and the LEN function can help you to remove the first 3 characters from your data cells. This method is described in the steps below. In cell C4, apply the RIGHT function nested with the LEN The formula is,

How to stop a space going into the input in JavaScript?

We could use the input’s pattern attribute and validate the input’s value after the user enters it. However, we want to do something while the user is entering the value to stop a space going into the input. So, we handle the keydown event and check whether the user has entered a space using event.key.

How to verify and restrict user input using regular expressions?

We can verify and restrict user input in two ways. The first method uses regular expressions and the second method uses ASCII value of characters. Let’s start with the first method. A regular expression is a sequence of a character used for pattern matching.

How to verify and restrict user input using jQuery?

Create a new file index.html and include jQuery before the closing </body> tag. We can verify and restrict user input in two ways. The first method uses regular expressions and the second method uses ASCII value of characters. Let’s start with the first method. A regular expression is a sequence of a character used for pattern matching.


2 Answers

Option 1 : Put the 3 character prefix outside of the input. This is the best from a user experience perspective.

<p>abc <input id="Lab" maxlength="10" name="LabWareId" type="text" class="valid"></p>

Option 2 : Check for backspace keypress and prevent the delete accordingly.

$(document).on('keydown', function (e) {
  if (e.keyCode == 8 && $('#Lab').is(":focus") && $('#Lab').val().length < 4) {
      e.preventDefault();
  }
});
like image 78
Ryan Dantzler Avatar answered Oct 07 '22 17:10

Ryan Dantzler


Try this (it's not the best solution but it works):

Fiddle

$(document).ready(function() {
  $("#tb").on("keyup", function() {
    var value = $(this).val();
    $(this).val($(this).data("initial") + value.substring(3));
  });
});

Mind you that if I use my mouse to highlight the first 3 characters and move them at the end of the other texts, it won't complain and allow it which is not what you want.

like image 20
Patrick Gregorio Avatar answered Oct 07 '22 17:10

Patrick Gregorio