- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript - Check if value is a percentage?
Let’s say the following is our value −
var value="97%";
To check the value for percentage, use regular expression.
Example
Following is the code −
var value="97%"; var result=/^\d+(\.\d+)?%$/.test(value); if (result==true) { console.log("The percent is="+value); } else { console.log("This is not percentage"); } var value1="percent"; var result1=/^\d+(\.\d+)?%$/.test(value1); if (result1==true) { console.log("The percent is="+value1); } else { console.log("This is not percentage"); }
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo214.js.
Output
The output is as follows on console −
PS C:\Users\Amit\JavaScript-code> node demo214.js The percent is=97% This is not percentage
Advertisements