I have a form with an input text field and I want to validate it before I submit the form.
You can enter a natural number like 1, 5, 7
etc or you can enter a float number like 1.5, 5.45, 7.38
How can I detect when the input has an Integer (natural) number or not?
If I’m using:
typeof($(input[name='myField']).val())
I always get it as a string.
I tried to convert the string to a number, but using parseInt
or parseFloat
doesn’t help, as I convert it directly from the code.
I need to know what type of number is the one in the string from the input field.
2
Answers
After a little more research and based on a few hints/comments on this question, i have fixed it with this simple trick (hope it will help others too).
Here are more details about this trick: Check if a number has a decimal place/is a whole number
Also you can use
isInteger
js function