Skip to content

Instantly share code, notes, and snippets.

@thewinterwind
Last active December 25, 2015 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thewinterwind/7036198 to your computer and use it in GitHub Desktop.
Save thewinterwind/7036198 to your computer and use it in GitHub Desktop.
HTML5 Number input helper for Laravel 4 Blade Templates with supporting Javascript
{{ Form::label('quantity', 'Quantity') }}
{{ Form::input('number', 'quantity', 0, array('class' => 'form-control required quantity', 'data-toggle' => 'tooltip', 'data-trigger' => 'focus', 'data-placement' => 'top', 'title' => 'Enter how many tags you would like to create', 'min' => '0', 'max' => '10000', 'maxlength' => '5')) }}
@section('scripts')
<script>
var inputQuantity = [];
$(".quantity").each(function(i) {
inputQuantity[i]=this.defaultValue;
$(this).data("idx",i); // save this field's index to access later
});
$(".quantity").on("keyup", function (e) {
var $field = $(this),
val=this.value,
$thisIndex=parseInt($field.data("idx"),10); // retrieve the index
// NOTE :invalid pseudo selector is not valid in IE8 so MUST be last
if (this.validity && this.validity.badInput || isNaN(val) || $field.is(":invalid") ) {
this.value = inputQuantity[$thisIndex];
return;
}
if (val.length > Number($field.attr("maxlength"))) {
val=val.slice(0, 5);
$field.val(val);
}
inputQuantity[$thisIndex]=val;
});
</script>
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment