Regex for percentage fields between 0 to 100 and accepting floating values

Kumar38
Kilo Sage

Hi ,

 

What is the regular expression for validating data percentage fields is between 0 to 100 and should accept floating values

1 ACCEPTED SOLUTION

Amit Pandey
Kilo Sage

Hi @Kumar38 

 

You can use the following regular expression-

 

^(100(\.0+)?|(\d{1,2})(\.\d+)?)$

 

Regards,

Amit

View solution in original post

4 REPLIES 4

Sumanth16
Kilo Patron

Hi @Kumar38 ,

 

Try below script:

 

if (isLoading || newValue == '') {
  return;
  }
  var regexp = "^(?:100.1(?:\.0)?|[0-100](?:\.[0-9])?|0?\.[1-9])$";
  if(!regexp.test(newValue)){
  alert('Please enter numeric value');
  g_form.setValue('v_sec_review_unknown','');
  }
}

 

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda

Amit Pandey
Kilo Sage

Hi @Kumar38 

 

You can use the following regular expression-

 

^(100(\.0+)?|(\d{1,2})(\.\d+)?)$

 

Regards,

Amit

Maddysunil
Kilo Sage

@Kumar38 

you can use:

^(100(\.0{1,2})?|[1-9]?\d(\.\d{1,2})?|0(\.[1-9])?|0)$

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Mark C_
Tera Expert

hi @Kumar38 ,

 

You may also check this code that test the captured value to the regex and alerts if is false.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    var percentageRegex = /^(100(\.0{1,2})?|\d{0,2}(\.\d{1,2})?)$/; // Regex for percentage between 0 and 100 with floating values

    if (!percentageRegex.test(newValue)) {
        // If the input does not match the regex, show an error message
        alert('Please enter a valid percentage between 0 and 100.');
        g_form.setValue(control.name, oldValue); // Reset the field value
    }
}

 

 

Cheers,

Mark