8

I want to prevent 0 as the first character. But when I focusout and focusin again, then I add "2" in the first, it still run, I want should not run.

Here is my HTML

<input type="number" id="test"> 

My JS

$('#test').keypress(function(evt) { if (evt.which == "0".charCodeAt(0) && $(this).val().trim() == "") { return false; } }); 

Anybody help or suggest what should I do? Thank you.

3
  • Are you trying to prevent the user from entering a value that begins with "0"? (So "02" would not be allowed but "20" would be allowed?) Because if so you can't do that by testing just the current keypress because the user may type the "2" then move the cursor in front of it and type the "0". (Also they may edit the field without using the keyboard, i.e., by pasting or drag'n'drop.) Commented Dec 6, 2016 at 2:11
  • why not change the value you get i think if you parse(parseint for example) in the value the leading zero will be removed Commented Dec 6, 2016 at 2:13
  • Hi @nnnnnn yes it was I mean. Commented Dec 6, 2016 at 2:13

5 Answers 5

16

You can use input event, which also handles pasted values, String.prototype.replace() with RegExp /^0/ to replace all 0 characters found within .value of element

$("#test").on("input", function() { if (/^0/.test(this.value)) { this.value = this.value.replace(/^0/, "") } })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </script> <input type="number" id="test">

Sign up to request clarification or add additional context in comments.

9 Comments

Hi @guest271314 that is not what I mean. Because I need only number,
This gives kind of a weird user experience if the user tries to edit an existing value by entering a zero in the middle, because it moves the cursor to the end of the string. Also, I think the OP wants to allow zeros as long as they're not the first character.
@nnnnnn "Also, I think the OP wants to allow zeros as long as they're not the first character." Ok. See updated post. Added ^ to beginning of RegExp; initially interpreted requirement for no 0 characters to be able to be entered. Question does not appear to indicate only first character that is 0 should not be allowed?
@dediwibisono See updated post. Added ^ to RegExp
@guest271314 - I wanted to add this edit but it's not long enough! Add + after 0 in case multiple zeroes are pasted, like so this.value = this.value.replace(/^0+/, "")
|
2

Compare which property with the ASCII code of number 0 and return false.

if (evt.which === 48) { return false; } 

Check Fiddle

2 Comments

Hi @Sushanth thank you for the answer. I'm sorry but I want to clear my question. your answer is disabled all "0". I mean "0" is only disabled in the first. When I typing "0" for the next it should be run. I hope you understand my question Thank you :)
Isn't the OP's code already making this comparison with the "0".charCodeAt(0) check?
1
$('#test').on("keypress",function (evt) { if (this.value.length == 1 && evt.which == 48 ) { evt.preventDefault(); } }); 

1 Comment

You'll be able to type 0 at the beginning if there's already some value (ex. type 77, move caret to the beginning, type 0)
1

Try

 if (event.target.value.length == 0 && event.which == 48) { return false; } 

Comments

1

You can simply add condition for this block "0" from first character using --> event.target.value. Here is the following code for block "0" from first character.

Javascript.

 const handleOnchange =()=>{ if(event.target.name === "myEvent"){ event.target.value === event.targe.value.replace(/^0/, ""); } } 

HTML.

<input type="number" name="myEvent" id="myEvent" onChange={handleOnchange} /> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.