number_field_tag(name, value = nil, options = {}) public

Creates a number field.

Options

  • :min - The minimum acceptable value.

  • :max - The maximum acceptable value.

  • :in - A range specifying the :min and :max values.

  • :within - Same as :in.

  • :step - The acceptable value granularity.

  • Otherwise accepts the same options as text_field_tag.

Examples

number_field_tag 'quantity'
# => <input id="quantity" name="quantity" type="number" />

number_field_tag 'quantity', '1'
# => <input id="quantity" name="quantity" type="number" value="1" />

number_field_tag 'quantity', nil, class: 'special_input'
# => <input class="special_input" id="quantity" name="quantity" type="number" />

number_field_tag 'quantity', nil, min: 1
# => <input id="quantity" name="quantity" min="1" type="number" />

number_field_tag 'quantity', nil, max: 9
# => <input id="quantity" name="quantity" max="9" type="number" />

number_field_tag 'quantity', nil, in: 1...10
# => <input id="quantity" name="quantity" min="1" max="9" type="number" />

number_field_tag 'quantity', nil, within: 1...10
# => <input id="quantity" name="quantity" min="1" max="9" type="number" />

number_field_tag 'quantity', nil, min: 1, max: 10
# => <input id="quantity" name="quantity" min="1" max="9" type="number" />

number_field_tag 'quantity', nil, min: 1, max: 10, step: 2
# => <input id="quantity" name="quantity" min="1" max="9" step="2" type="number" />

number_field_tag 'quantity', '1', class: 'special_input', disabled: true
# => <input disabled="disabled" class="special_input" id="quantity" name="quantity" type="number" value="1" />
Show source
Register or log in to add new notes.
March 19, 2015 - (v4.0.2)
0 thanks

number_field tag does not accept the same options as text_field_tag

number_field_tag does not accept the size or maxlength options; the max option for number_field_tag is used to control the size of the field. According to W3 http://www.w3.org/TR/html-markup/input.number.html , placeholder should be accepted, but I don’t find that this works in the way it works for text_field_tag.