Forcing users to type in only numbers in a field (RoR)
There is similar question like this for ASP.net, but I wanted to know if there is a 'relatively' easy way to do this on RoR.
I ha开发者_StackOverflow社区ve a field for "Price" where I want the user to only type in numbers. This means that when the user tries to type in anything else, my web app shouldn't let them (nothing should happen).
If this isn't possible, I guess I can just validate my data after the user has submitted the "Submit" button and then flash a message that says "'Price' must be a number."
Thank you for your time,
JHS
This requires a javascript solution (since you want the check to occur client-side) so it will depend on which javascript framework you are using. If you are using jQuery this is a nice one that will do exactly what you need: Numeric
as everyone already mentioned this requires a javascript solution. you can use the following regex to test that only numbers are given
/^[0-9]+$/
tie that to an onKeyUp event handler and you can validate the input client side
精彩评论