Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex expression that requires a user to input a number as a %

Can’t figure out how to create a regex expression that requires the user to input a number as a %

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It’s very simple, it looks like this:

const val1 = "10";
const val2 = "10%";

const checkPercentage = val => /\d+%$/.test(val);

console.log(checkPercentage(val1));
console.log(checkPercentage(val2));

\d+ represents between 1 digit and +

%$ means that the "%" sign must be at the end of the string.

😉

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading