Regex - pricing validation for MySQL decimal (12,2)

0

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:

  • Accept a value where there are only numbers or digits and ".".
  • Does not only accept ".".
  • If there is "." there can only be two digits at most after it.
  • Before the "." only a maximum of 10 digits may exist.
  • If there is no "." only a maximum of 10 digits may exist.
  • For this filter I have the following regex: ^\d{0,8}?(\.)?(\d{1,2})?$ link

    It is working for all scenarios except for these:

  • When you do not have "." it accepts up to 12 digits. So 12345678901 should not accept because it has 11 figures.
  • The point alone is being accepted but should not "."
  • 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({
    
        
    asked by anonymous 19.05.2016 / 18:08

    1 answer

    0

    Okay, here's the pattern you're looking for. I'll give it to anyone who has an interest.

    ^(?!\.$)\d{0,10}(?:\.(?:\d\d?)?)?$
    

    link

    And here's the full function to validate this type of entry:

    $('#txt_preco').bind({             
    
                 keyup:function(){
    
                       var objectEvent=$(this);
                       var ck_input = /^(?!\.$)\d{0,10}(?:\.(?:\d\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({
    
        
    19.05.2016 / 20:53