-1

Is there an easy way to allow users to enter only integers positive and negative. I can do it with few lines of JavaScript or jQuery but hope there is an easy way like regex. But I could not found what I need. I need to be able to enter these characters: -0123456789

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

I don't need dot/coma/exp char. Only -0123456789, i.e. positive and negative integers

John Glabb
  • 1,336
  • 5
  • 22
  • 52
  • Isn't that what `type="number"` does? I don't get the question – Andy Aldo Oct 22 '17 at 04:07
  • Possible duplicate of [How to allow only numeric (0-9) in HTML inputbox using jQuery?](https://stackoverflow.com/questions/995183/how-to-allow-only-numeric-0-9-in-html-inputbox-using-jquery) – Mohamed Chaawa Oct 22 '17 at 04:07
  • Didn't you do any research before asking this? https://stackoverflow.com/questions/37233040/dont-let-a-user-type-the-period-character-in-an-html5-input-type-number – NewToJS Oct 22 '17 at 04:07
  • As I said I don't want to use jQuery.. And HTML code above allows to enter dot and 'e' that I don't want to – John Glabb Oct 22 '17 at 04:09
  • @JohnGlabb The regex on the link I posted will work if you modify it to `/[\.,]/g` – NewToJS Oct 22 '17 at 04:24

4 Answers4

2

<input id='number' type='number' pattern="[0-9\-]+">
kemotoe
  • 1,730
  • 13
  • 27
0

There is a pattern attribute where you can specify your own regex for the input. For more info, here. It is the closest thing you can get if you do not want to write any javascript

Andy Aldo
  • 890
  • 1
  • 9
  • 24
0

You can use pattern attribute with RegExp [0-9-]|[^e] and required attribute within a <form> element

<form>
  <input type="number" pattern="[0-9-]|[^e]" required>
  <input type="submit">
</form>
guest271314
  • 1
  • 15
  • 104
  • 177
  • @JohnGlabb _"it doesn't work.. It allows enter letters."_ At which browser? The JavaScript artifact E-notation `e` can be entered, though not submitted – guest271314 Oct 22 '17 at 04:22
  • FireFox doesn't work at all.. In Chrome it allows enter dot and 'e' – John Glabb Oct 22 '17 at 04:23
  • @JohnGlabb _"In Chrome it allows enter dot and 'e' "_ At Chromium 61 E-notation `e` can be entered, but the `
    ` will not be submitted when `e` or `.` is within the value. At which version of Chrome did you try stacksnippets? _"FireFox doesn't work at all.."_ Just tried at Firefox 57, with same results; that is, the `
    ` is not submitted if either `e` or `.` is within `value` of `` as the value will be invalid when matched against the `RegExp` at `pattern` attribute. Not certain what you mean by "doesn't work at all"?
    – guest271314 Oct 22 '17 at 04:25
  • @JohnGlabb Does the `
    ` submit when either or both `e` and `.` are included at value of `` at the browsers you tried code?
    – guest271314 Oct 22 '17 at 04:30
  • I don't want to allow dot and 'e' at all. FF - 56.0.1, Chrome - 61.0.3163.100. Seems Being Sunny has a solution.. See below – John Glabb Oct 22 '17 at 04:32
  • @JohnGlabb Do you mean that you are trying to prevent user from typing `e` and `.` at all? What is the purpose of this sentence at Question _"I can do it with few lines of JavaScript or jQuery but hope there is an easy way like regex."_ ? You already had a solution "with few lines of JavaScript " that you had written before posting Question, yes? What exactly is the issue with the code that you had previously written? – guest271314 Oct 22 '17 at 04:35
  • well, I'm looking for e.g. pure regex solution instead of this: – John Glabb Oct 22 '17 at 05:50
  • @JohnGlabb What do you mean by "pure regex solution"? Where should `RegExp` be used? – guest271314 Oct 22 '17 at 05:59
  • Ok.. I mean pure HTML to use e.g. type, pattern, e.t.c.. or any other attributes. Why it's so complicated to implement so I need to use JavaScript. I thought there is an easy solution, like a control property – John Glabb Oct 22 '17 at 06:14
  • @JohnGlabb _"I mean pure HTML to use e.g. type, pattern, e.t.c.. or any other attributes."_ Why do you expect HTML alone to be able to implement "allow users to enter only integers positive and negative"? – guest271314 Oct 22 '17 at 06:54
0

Actually when you use type="number", it'll not do what you want, I have a solution for you using type="text"

<input type="text" oninput="this.value = this.value.replace(/[^0-9-]/g, ''); this.value = this.value.replace(/(\..*)\-/g, '$1');" >
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • this example works fine but I thought there is better approach to use pure regex. I'll mark it as the answer – John Glabb Oct 22 '17 at 04:28