I want to make change on input field using jQuery in such order when the user pasted a text which starting with '0' character. In other words if for example the user pasted '0' it will be changed to '1'.
My entire HTML code is like below:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#amount").bind("paste", function(evt) {
var pastedData = evt.originalEvent.clipboardData.getData('text');
if (pastedData.charAt(0).length != 0 && pastedData.charAt(0) == '0') {
$("#amount").val("1");
alert("you entered " + pastedData + "; but we can't change it to 1!");
};
});
});
</script>
</head>
<body>
paste 0 in this field. it shall be changed to 1, but the result will be 10!<br>
<input type="text" id="amount" placeholder="enter amount...">
</body>
</html>
But the problem is, when I paste '0' it will not change to '1'; it will be changed to '10'. Or if you enter '00000' it will be '100000'. In other words the code just prepend '1' to my pasted text.
Now can anyone tell me what's wrong with my code?
Thanks.