Reputation: 141
I want to make an input field where the user can only enter a number. In HTML5, we can use <input type="number">
. How do I do this in blade?
I tried this:
{!! Form::number('amount', null, array('class' => 'form-control')) !!}
Upvotes: 3
Views: 24743
Reputation: 5649
You can use either Form::input()
method or Form::number()
method to achieve your goal.
Form::input() method
This method takes 4 arguments.
Example:
{{ Form::input('number', 'name') }}
{{ Form::input('number', 'name', 'value', ['class' => 'number', 'id' => 'numberField']) }}
//both example will create elements like this
<input name="name" type="number" value="value">
<input name="name" type="number" value="value" class="number" id="numberField">
Form::number() method
This method takes 3 arguments.
Example:
Form::number('name')
Form::number('name', null, ['class' => 'number', 'id' => 'numberField'])
//both example will create elements like this
<input name="name" type="number">
<input name="name" type="number" class="number" id="numberField">
Tips: if you want to use
$options
and don't want to assign any default value, usenull
at the$value
argument.
Upvotes: 5
Reputation: 71
{!! Form::input('number', 'weight', null, ['id' => 'weight', 'class' => 'form-control', 'min' => 1, 'max' => 9999, 'required' => 'required']) !!}
Upvotes: 0
Reputation: 457
You can use javascript:
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
and in your html:
<input name="input_name" onkeypress="return isNumberKey(event)">
Upvotes: -1
Reputation: 179
Input field with minimum value of 0.
{!! Form::number('count', $value = '' , ['min' => '0' ,'class' => 'form-control', 'id' => 'number_count','required']) !!}
Upvotes: 1
Reputation: 141
I could search and code it. Since there was no direct answer, I thought to post the working code below:
{!! Form::input('number', 'amount', null, ['class' => 'form-control']) !!}
Upvotes: 9