Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 - Make text field accept only numeric values

How do I make a text field accept only numeric values? If I press a letter or a symbol, the text field should not be filled, it should only allow numbers.

Is there a rails way to do this?

like image 882
Angelo Avatar asked Dec 12 '11 21:12

Angelo


People also ask

How do I make text fields only accept numbers?

You can use the <input> tag with attribute type='number'. This input field allows only numerical values. You can also specify the minimum value and maximum value that should be accepted by this field.

How do I restrict input to numbers in HTML?

To limit an HTML input box to accept numeric input, use the <input type="number">. With this, you will get a numeric input field. After limiting the input box to number, if a user enters text and press submit button, then the following can be seen “Please enter a number.”


1 Answers

Use number_field_tag, this will generate a HTML5 number field

http://apidock.com/rails/ActionView/Helpers/FormTagHelper/number_field_tag

like image 68
Thong Kuah Avatar answered Oct 11 '22 23:10

Thong Kuah