Question

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 have 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

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top