0

I create a form, where one field should only be filled with numbers.

I want that field can be filled only with numbers since entering input.

Like this example :

How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?

I've tried using Regex, but when I try to input is still able to enter letters.

<input type="number" min="2" pattern="^[0-9]" class="andi_input required-entry" name="amount" id="amount" required title='Only Number' />

I want it when input to field and not after click the Submit button and the message appear and inform that the field can only be filled with numbers.

I also try to add validate-number, but the result is the same.

How, without javascript, so that the field can only be filled with numbers or letters?

Whether for this kind of case have to use JavaScript or is there another way without JavaScript?

2
  • You either use HTML5 validation which occurs on submit. Or you use Javascript (or a library that uses Javascript) Commented Nov 24, 2015 at 8:30
  • So had to use javascript? There is no other way without javascript? Commented Nov 25, 2015 at 4:47

3 Answers 3

2

HTML 5 uses the type="number" so make sure that the browser that you are using is compatible. Check it out in action here.

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

2 Comments

Thank you @travis-m for the reply / suggestion, but I mean / want is like this example How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?, where since the beginning of the input can not enter letters (numbers only), not after click Submit button
Sorry @travis-m, I want to click 'This answer is useful' to your answer but my reputation score is still less.
1

You should check browser compatibility. You're right with <input type="number" pattern="^[0-9]" />.

Your regex rule is : ^[0-9] :

  • ^ assert position at start of the string
  • [0-9] match a single character present in the list below (0-9 a single character in the range between 0 and 9)

You can check your regex here.

I use to use HTML5 Validation and jQuery one because IE is capricious most of the time.

UPDATE :

Without Javascript it's not possible to check pattern on real time. I suggest you to use a jQuery library like : http://www.jqueryscript.net/form/jQuery-Plugin-For-Formatting-User-Input-with-Specified-Pattern-formatter-js.html.

4 Comments

Thank you @delphine for the reply / suggestion, but I mean / want is like this example How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?, where since the beginning of the input can not enter letters (numbers only), not after click Submit button
I understand but without Javascript it's not possible and it was mentionned in your post title. I suggest you to use a jQuery library like : jqueryscript.net/form/…. Script in the link is working for you ?
Thank you @delphine, so the answer is without Javascript it's not possible :) I want to click 'This answer is useful' but my reputation score is still less. I'll accept your answer as an answer to approach the question.
I updated my answer so everyone can see the good solution and maybe you can tick it !
0

Here is a OneLiner:

<input type="number" onkeypress="return /[0-9]/i.test(event.key)">

or

<input type="text" onkeypress="return /[a-z]/i.test(event.key)">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.