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();
});
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,
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.
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.
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.
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();
}
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With