How to Allow Only Numeric Value in HTML Text Input Using jQuery
All Answers
total answers (1)
Severity: 8192
Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated
Filename: libraries/Filtered_db.php
Line Number: 23
total answers (1)
Use the RegExp
test()
MethodIf you want to allow your users to enter only numeric value in an HTML text input field, you can create a regular expression and test the input value against it using the
test()
method.Let's try out the following example which applies a red outline to the text input box if the value entered is not integer or float, applies green outline otherwise:
Note that the input event fires on keyboard input, mouse drag, autofill and even copy-paste. But, it will not fire if you paste the same string or select and retype the same character inside the input.
If you want to allow only numeric characters (0-9) in HTML text input, use the regex /^\d*$/. In the above example if you also want to allow negative values you can use the regex /^-?\d*[.]?\d*$/.
need an explanation for this answer? contact us directly to get an explanation for this answer