skip to Main Content

I’m unable to do or find a regex that allows me to insert in an input numbers between 0 and 100, and also allow to insert up to 2 decimals.

I can have them for separated but can’t merge them. I’m starting to think that there is an inconsistence

  const VALIDATION = {
    ONLY_NUMS: (value: string) => /^[0-9]*$/.test(value),
    BETWEEN_0_AND_100: (value: string) => /^(d{0,2}([.,]d{1,2})?|100([.,]0{1,2})?)$/.test(value),
    FLOAT_NUMS: (value: string) => /^-?[0-9]*((?<=d).(?=d))?[0-9]*$/.test(value) && value !== '-',
  };

2

Answers


  1. If your validation logic is already a function, you can parse the string using a leading +n or calling Number(n) to coerce the string into an number.

    Now that it’s a number, you can check if it’s !NaN and in the range.

    const INT_EXPR = /^-?[0-9]+$/;
    const FLOAT_EXPR = /^[+-]?([0-9]*[.])?[0-9]+$/;
    
    const inRange = (n: Number, min: Number, max: Number): Boolean => n >= min && n <= max;
    
    const Validation = {
      IS_INTEGER: (value: string) => INT_EXPR.test(value),
      BETWEEN_0_AND_100: (value: string) => !isNaN(+value) && inRange(+value, 0, 100),
      IS_FLOAT: (value: string) => FLOAT_EXPR.test(value),
    } as const;
    
    const validCases = [
      Validation.IS_INTEGER('42'),
      Validation.IS_INTEGER('-32'),
      Validation.BETWEEN_0_AND_100('2'),
      Validation.IS_FLOAT('2'),
      Validation.IS_FLOAT('2.01'),
    ];
    
    const invalidCases = [
      Validation.IS_INTEGER('-'),
      Validation.IS_INTEGER('3.14'),
      Validation.BETWEEN_0_AND_100('-1'),
      Validation.BETWEEN_0_AND_100('101'),
      Validation.IS_FLOAT('-'),
      Validation.IS_FLOAT('A'),
    ];
    
    // Validate all
    function identity<TObj>(obj: TObj): TObj {
      return obj;
    }
    
    console.log(validCases.every(identity) && !invalidCases.some(identity));
    

    Compiled snippet

    const INT_EXPR = /^-?[0-9]+$/;
    const FLOAT_EXPR = /^[+-]?([0-9]*[.])?[0-9]+$/;
    
    const inRange = (n, min, max) => n >= min && n <= max;
    
    const Validation = {
        IS_INTEGER: (value) => INT_EXPR.test(value),
        BETWEEN_0_AND_100: (value) => !isNaN(+value) && inRange(+value, 0, 100),
        IS_FLOAT: (value) => FLOAT_EXPR.test(value),
    };
    
    const validCases = [
        Validation.IS_INTEGER('42'),
        Validation.IS_INTEGER('-32'),
        Validation.BETWEEN_0_AND_100('2'),
        Validation.IS_FLOAT('2'),
        Validation.IS_FLOAT('2.01'),
    ];
    
    const invalidCases = [
        Validation.IS_INTEGER('-'),
        Validation.IS_INTEGER('3.14'),
        Validation.BETWEEN_0_AND_100('-1'),
        Validation.BETWEEN_0_AND_100('101'),
        Validation.IS_FLOAT('-'),
        Validation.IS_FLOAT('A'),
    ];
    
    // Validate all
    function identity(obj) {
        return obj;
    }
    
    console.log(validCases.every(identity) && !invalidCases.some(identity));
    Login or Signup to reply.
  2. If you truly can’t avoid to use regexp, this one should do the trick:

    (^0|^[1-9][0-9]?|^100)($|(.|,)[0-9][0-9]?$)
    

    It allows even decimal after 100 (like 100.34)
    The below one is up to 100 with no decimal allowed after (max 99.99):

    (^100$|^0|^[1-9][0-9]?)($|(.|,)[0-9][0-9]?$)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search