I want to delete multiple "0" when inputing in input html, necessarily, need use keypress, at the same time delete. For example when inputing 000000000 = 0, Instantly delete each symbol, or 000562 = 562. Thank's!
Asked
Active
Viewed 67 times
-4
-
What have you tried, where is your attempt(s) and in what way doesn't it appear to be functioning as intended? You can't just write a "I want" list and have people write it for you. This isn't a free writing service. – NewToJS Jul 27 '17 at 03:31
-
You need to at least try to write some code and solve the problem first. – Difster Jul 27 '17 at 03:31
-
Please add the functions that you've already implemented with regard to this issue – Nayantara Jeyaraj Jul 27 '17 at 03:32
-
Possible duplicate of [Javascript parseInt() with leading zeros](https://stackoverflow.com/questions/8763396/javascript-parseint-with-leading-zeros) – Alon Eitan Jul 27 '17 at 03:35
-
@AlonEitan - This has nothing to do with that other question. – nnnnnn Jul 27 '17 at 03:54
-
I wouldn't recommend doing this on keypress, because (even aside from the fact that users can edit input values without using the keyboard) trying to programmatically modify the value as they type means you have to maintain the current cursor position and so forth. Also if the user initially types "50000" by mistake and then tries to change the "5" to a "4" your auto-zero-removal behaviour would remove the zeros even though it shouldn't. Just remove the leading zeros on the change or blur event, easily done with the string `.replace()` function. – nnnnnn Jul 27 '17 at 03:56
-
@nnnnnn But `parseInt('000562',10)` will give the same result as the user is asking for, or have I missed anything? – Alon Eitan Jul 27 '17 at 04:01
-
The parseInt() function may provide the result the OP wants, but for reasons completely unrelated to what that other question was asking about. – nnnnnn Jul 27 '17 at 04:06
-
@nnnnnn I retracted my CV, but I still think that duplicate might have been helpful, since the OP shows zero efforts and research, and I couldn't find more accurate question – Alon Eitan Jul 27 '17 at 04:11
2 Answers
1
You could do something like this:
HTML:
<input type="text" id="myInput">
JS:
var stripZeroesRegex = /^0+/;
function stripZeroes(str) {
return str.replace(stripZeroesRegex, '0');
}
myInput.addEventListener('input', function(ev) {
if (stripZeroesRegex.test(this.value)) {
this.value = stripZeroes(this.value);
}
});

hayzey
- 464
- 2
- 9
0
OnKeyPress = "return onlyNumbers(this.id)"
function onlyNumbers(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode == 48) {
return false;
}
return true;
}
when user press 0 then return false I don't know about how to remove but I know how to varify 0 pressed or not that's why I mention this method.

Neeraj Pathak
- 759
- 4
- 13