Mark Hill
Mark Hill

Reputation: 1839

Regular Expression still allowing decimal at the end

Hey Everyone I'm working in angular and I am trying to create a filter that just allows numbers in the format of "any amount of digits . any amount of digits" Otherwise is will prevent the entry if it's not in the format of [0-9.]. The pattern is still allowing numbers like this 000.00. in the input field.

How do I stop it from inserting that last decimal point? Thank you for your assistance ahead of time!

code

$scope.filterValue = function($event, value){
    var char = String.fromCharCode($event.keyCode);
    if(value === null){
        if(isNaN(char)){
            $event.preventDefault();
        }
    }else{  
        var pattern = /^-?[0-9]{0,}\.?[0-9]{0,}$/;
        if(pattern.test(value)){
            if(isNaN(char) && (value.indexOf(".") == value.length-1)){
                $event.preventDefault();
            }
        }else{
             $event.preventDefault();
        }
    }
};

Upvotes: 0

Views: 137

Answers (3)

user557597
user557597

Reputation:

These regex should cover what you probably need.
The overall assumption is there has to be input so we use
an alternation to positively check the forms.

This doesn't require a dot

^-?(?:\d+(?:\.\d*)?|\.\d+)$

 ^ 
 -?
 (?:
      \d+                  # Digits 
      (?: \. \d* )?        # Optional  ( dot, optional ( digits ) )
   |                     # or,
      \. \d+               # Dot, digits 
 )
 $

This requires a dot

^-?(?:\d+\.\d*|\.\d+)$

 ^ 
 -?
 (?:
      \d+                  # Digits 
      \. \d*               # Dot, optional digits 
   |                     # or,
      \. \d+               # Dot, digits 
 )
 $

Upvotes: 0

VLAZ
VLAZ

Reputation: 29087

I will reference an article on floating point and regex which was written by people way smarter than me. Here is the regex suggested there:

^[-+]?[0-9]*\.?[0-9]+$     

And an explanation - it defines both the optional and the mandatory input. In this case you want to have at least one digit which is the final [0-9]+ but you also allow for more digits and an optional dot character. The regex also allows a number to start with a + or a -

See this in regex101

var regex = /^[-+]?[0-9]*\.?[0-9]+$/;

var test = function(input) {
  console.log("Testing ["+ input + "]. Result: " + (regex.test(input) ? "passes" : "fails"));
}

//valid
test("1");
test("2.1");
test("3.14");
test("-4");
test("+5");
test(".6");
test("-.7");
test("+.8");

//invalid
test(""); // yes - empty string
test(".");
test("-");
test("+");
test("-.");
test("+.");
test("9.");
test("10..1");
test("11.1.2");
test(".12.1");
test("..13");
test("-.14.");
test("+..15");

Upvotes: 2

KrishCdbry
KrishCdbry

Reputation: 1059

$scope.filterValue = function($event, value){
var char = String.fromCharCode($event.keyCode);
if(value === null){
    if(isNaN(char)){
        $event.preventDefault();
    }
}else{  
    var pattern = /^-?([0-9]{1,})(?:\.[0-9]+)?$/;
    if(pattern.test(value)){
        if(isNaN(char) && (value.indexOf(".") == value.length-1)){
            $event.preventDefault();
        }
    }else{
         $event.preventDefault();
    }
}
};

Try this one

var regex = /^-?([0-9]{1,})(?:\.[0-9]+)?$/;

Upvotes: 0

Related Questions