admin管理员组

文章数量:1415100

I need to allow user to only enter integer (33) and float (33.343) in a textbox.

Below is my Regex Query but it is not working fine. It is allowing the user to enter float values but giving error on int values.but i need to allow user to enter int also. It is working fine and giving error message when user enter any other type of value.

/^[0-9]*[.][0-9]*$/

I need to allow user to only enter integer (33) and float (33.343) in a textbox.

Below is my Regex Query but it is not working fine. It is allowing the user to enter float values but giving error on int values.but i need to allow user to enter int also. It is working fine and giving error message when user enter any other type of value.

/^[0-9]*[.][0-9]*$/
Share Improve this question edited May 23, 2018 at 15:23 Ωmega 43.7k35 gold badges142 silver badges212 bronze badges asked Oct 18, 2012 at 12:43 Asp_NewbieAsp_Newbie 2692 gold badges9 silver badges17 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 3

Make dot and the part after it optional:

"^\d+(?:\.\d+|)$"

Also prefer \d over [0-9], don't use * since then you allow the possibility of no number either side of the .

This will accept:

1
1.23

but not:

.1
23.

It's up to you if you want that behaviour.

Try with:

/^[0-9]+(?:\.[0-9]+)?$/

Use regex pattern

^(?=.*\d)\d*(?:\.\d*)?$

This will match for example:

1234
123.
1.23
12.3
0.12
.123

本文标签: javascriptRegular expression for int and float both not working fineStack Overflow