Last active
December 25, 2015 20:39
-
-
Save thewinterwind/7036198 to your computer and use it in GitHub Desktop.
HTML5 Number input helper for Laravel 4 Blade Templates with supporting Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ 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