1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| $(':input.two-digits[type="number"]')
.on('keypress',function(e){
/*if(e.ctrlKey==true && e.charCode == 118){
$(this).trigger('focusout');
return this;
}*/
if($.inArray(e.keyCode,[37,39,35,36,8,9,46])==-1){
//var input = String.fromCharCode(e.charCode);
var regex = new RegExp("[0-9\.]");
if (regex.test(e.key)) {
return this;
} else {
e.preventDefault();
}
} else {
return this;
}
})
.on('keyup',function(e){
if($(this).val().indexOf('.')!=-1){
if($(this).val().split(".")[1].length > 1){
if( isNaN( parseFloat( this.value ) ) ) return;
this.value = parseFloat(this.value).toFixed(2);
}
}
return this; //for chaining
})
.on('focusout',function(e){
var txt =$(this).val();
//console.debug(regex.test('[-+]?([0-9]*.[0-9]+|[0-9]+)'));
var regex = new RegExp(/^[+-]?(?:[1-9]\d*|0)?(?:\,\d{1,2})?$/gm);
if($(this).val()!=""){
$(this).removeClass('input_grand_obligatoire');
if(regex.test($(this).val())){
$(this).parent().removeClass('has-error');
} else {
$(this).parent().addClass('has-error');
$(this).val("");
};
}
}); |
Partager