Link to home
Start Free TrialLog in
Avatar of enigma1234567890
enigma1234567890Flag for Ireland

asked on

regex for numbers only

what is the regex to check if a input is only numbers for example a telephone number must only have telephone numbers not digits.  The regex should only work if something is entered.  

Please explain example
Avatar of Saikapian_4739
Saikapian_4739
Flag of India image

If u want to check only for numbers the following with work.

^[0-9]*$

This will make sure that the input starts and ends with numbers only and at the same time will check that the input contains only numbers b/w 0-9 no matter how many times they come.

Like regex will work for 4578, 12 578978787 but not t5656, 6767y, 767hju767, and for any other input that will contain anything other than a number.
Avatar of enigma1234567890

ASKER

didnt work only checks for all digits  not 666h or 445ft44
That should not match 666h, unless something is fishy in denmark.  it would match blanks though.  A '+' should fix that:
^[0-9]+$

What regex rules are you using.  Same as this link?

http://regexlib.com/CheatSheet.aspx
Also if you post the exact code/command/sample, that might help.
its in a javascript and looks like.  I added {1,} to make sure blank requests dont get called

^[0-9]{1,}
not if I enter 66666666 or abcdefg it work
ASKER CERTIFIED SOLUTION
Avatar of Bryan Butler
Bryan Butler
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
so what should it look like with the + ???
^\d+$

The {1,} or the + should be interchangeable.

doesnt work  I think the sintax is right so will close question and opena s a javascript qustion to see what is wrong with code
thanks all