1

I am using a regex to display a tooltip when the user's pattern doesn`t match desired pattern.

The RULES are:

  1. Must match 1 to 10 digits or less. Ex: 1234567890 or 0 or 123... . 0 digits is empty. Not allowed.
  2. Must match max 10 digits or less and a dot. Ex: 1234567890. or 12. or 1. or 0.
  3. Must match max 10 digits or less and a dot. and 1 or 2 digits after dot. Ex: 1.1 or 1234.23 or 1234567890.1 or 1234567890.12 OR .1 OR .21
  4. Only . is not allowed.

So far I have this pattern ^\d{0,8}?(\.)?(\d{1,2})?$ Link but I can not realize how to match all conditions at once.

This pattern can not match 1234567890.12 (it should). And it is matching the dot alone .(should not). Some post I found do not solve the issue link.

What am I missing?


The complete function

$('.validation').bind({             

             keyup:function(){

                   //var objectEvent=$(this);
                   var objectId=$(this).attr("id");
                   var objectEvent=$("#"+objectId);


                   if(objectId=="txt_Attend"){
                          var ck_input = /^\d+$/;
                          var msg="Oops! Only numbers 1-9 are allowed";
                          var extraVal ="0";
                   }
                   else if(objectId=="txt_SalesYTD"||objectId=="txt_PrevSales"){//alert(objectId);
                          //var ck_input = /^\d{0,10}((\.\d{1,2})?|(?<=\d)\.)$/;
                          var ck_input = /^(?!\.$)\d{0,10}(?:\.(?:\d\d?)?)?$/;
                          var msg="Enter a valid value. Ex: 123.35";
                          var extraVal ="";
                          //alert(ck_input);
                   }
                   var input = $.trim(objectEvent.val());
                   var validationTest =ck_input.test(input);   
                   //alert(validationTest);
                   //QTY field Validation --------------------------------------------------------------------------------------------------

                   if(!validationTest||input===extraVal){//If not match ck_input
                         //alert("algo errado"+input);                                                        
                                 objectEvent.val('');//Clear input field
                                 objectEvent.attr('data-original-title',msg);
                                 objectEvent.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 $('.validation').bind({

2 Answers 2

1

You need to change the first rage to {0,10} and put the dot and second numbers in same capture group with an optional dot that must be precede by a digit:

^\d{0,10}((\.\d{1,2})?|(?<=\d)\.)$

See demo: https://regex101.com/r/jG3uK7/4

1
  • Hi Kasramvd your pattern is not matching .1 and .12
    – IgorAlves
    May 19, 2016 at 18:46
1

You can use this

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

Regex Demo

7
  • Hi Rock, Rule 3. Your patter is not matching .1 and .12
    – IgorAlves
    May 19, 2016 at 18:47
  • @zwitterion see modified regex
    – rock321987
    May 19, 2016 at 18:49
  • Hi Rock your patter is matching the dot. Rule 4. Not allowed. Kasramvd found the good pattern
    – IgorAlves
    May 19, 2016 at 18:57
  • @zwitterion see the updated regex..i think it should suffice all condition now
    – rock321987
    May 19, 2016 at 19:00
  • @zwitterion alternation takes more step..this solution will be more efficient than accepted one
    – rock321987
    May 19, 2016 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.