Regular Expression for number only | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Regular Expression for number only

I use [0-9] expression to validate text field have number only. If you write abc 900 so it gives error, but i you write 900 abc , it gives no error. What's correct regular expression for number only Please help.

27th Feb 2017, 8:27 AM
Salman Mushtaq
Salman Mushtaq - avatar
4 Answers
+ 5
What message "never remove"? How do you handling your input validation? in which context? If you are validating inputs from html <input> through the attribute pattern, you can do the behavior you describe: show/hide an error message... suppose you have: <label>Choose a number:</label><br> <input type="text" pattern="^\d+
quot;><br> <label>error message</label> input+br+label { opacity:0; color:red; } input:invalid+br+label { opacity:1; } However, you can use the <input type="number"> for this special case ;) ... If you handle regexp in html form context... but you don't specify :P
28th Feb 2017, 12:52 AM
visph
visph - avatar
+ 2
^\d+$ or ^[0-9]$
27th Feb 2017, 8:30 AM
Ilya
Ilya - avatar
0
Thank you its work with ^[0-9]*$
27th Feb 2017, 8:43 AM
Salman Mushtaq
Salman Mushtaq - avatar
- 1
IIya, now it shows validation message but if user correct it the error message never remove
27th Feb 2017, 8:39 AM
Salman Mushtaq
Salman Mushtaq - avatar