I have a form where there is a price field that will be populated by the user. The goal is to accept some entries and block others (validation). The value will be stored in a MySQL database in a decimal (12,2) field. This means that this field will accept a total value of 12 digits, 2 decimals. Ex1: 1111111111 will be saved in MySql as 1111111111.00. That is, even if the user does not enter the cents, the accuracy of .00 will be saved. Ex2: 1234567890.00 is a valid value while 12345678901.00 is not therefore exceeded 12 digits. Ex3: .23 is a valid value since MySql transforms to 0.23 within the table. But "." (only the dot) is not valid, whereas .0 is valid (will be saved as 0.00).
So the rules are as follows:
For this filter I have the following regex:
^\d{0,8}?(\.)?(\d{1,2})?$
link
It is working for all scenarios except for these:
At most everything seems ok. Does anyone know how to solve this?
NOTE:
Although it's not important for this question, I'm going to post the javascript function that uses the regex parameter. It is working properly. Adds a bootstrap tooltip when the user inserts something not expected by the regex standard
$('#txt_preco').bind({
keyup:function(){
var objectEvent=$(this);
var ck_input = /^\d+$/;
var input = $.trim(objectEvent.val());
var validationTest =ck_input.test(input);
//QTY field Validation --------------------------------------------------------------------------------------------------
if(!validationTest||input==='0'){//If not match ck_input
//alert("algo errado"+input);
objectEvent.val('');//Clear input field
$(this).attr('data-original-title','Oops! Only numbers 1-9 are allowed');
$(this).tooltip('show');
setTimeout( function(){
objectEvent.tooltip('hide');
objectEvent.removeAttr('title');
objectEvent.removeAttr('data-original-title');
} , 2500 ); //Wait 2,5 seconds
}
///////////////////////////////////////////////////////////////////////////////////////////
else{//If validation match ck_input
objectEvent.tooltip('hide');
objectEvent.removeAttr('title');
objectEvent.removeAttr('data-original-title');
}//End of else if(!validationTest||input==='0'){
}//Fim keyUp
});//End of $('.txt_qty').bind({