0

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

4

2 回答 2

1

这需要一个 javascript 解决方案(因为您希望在客户端进行检查),因此它将取决于您使用的 javascript 框架。如果您使用的是 jQuery,这是一个不错的选择,可以满足您的需求:Numeric

于 2011-02-23T23:35:13.697 回答
1

正如大家已经提到的,这需要一个 javascript 解决方案。您可以使用以下正则表达式来测试只给出数字

/^[0-9]+$/

将其绑定到 onKeyUp 事件处理程序,您可以验证输入客户端

于 2011-02-23T23:39:54.263 回答