

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding the validity of a hex code in JavaScript
A string can be considered as a valid hex code if it contains no characters other than the 0-9 and a-f alphabets.
For example:
'3423ad' is a valid hex code '4234es' is an invalid hex code
We are required to write a JavaScript function that takes in a string and checks whether its a valid hex code or not.
Example
The code for this will be −
const str1 = '4234es'; const str2 = '3423ad'; const isHexValid = str => { const legend = '0123456789abcdef'; for(let i = 0; i < str.length; i++){ if(legend.includes(str[i])){ continue; }; return false; }; return true; }; console.log(isHexValid(str1)); console.log(isHexValid(str2));
Output
The output in the console will be −
false true
- Related Questions & Answers
- Check for Valid hex code - JavaScript
- Checking the validity of parentheses in JavaScript
- Checking validity of equations in JavaScript
- Finding the length of a JavaScript object
- C# program to check the validity of a Password
- Python program to check the validity of a Password?
- Finding the ASCII score of a string - JavaScript
- Finding the number of words in a string JavaScript
- Setting validity of a user in SAP HANA
- Finding the balance of brackets in JavaScript
- Finding the inclination of arrays in JavaScript
- HTML5 validity of nested tables
- Finding the length of the diagonal of a cuboid using JavaScript
- ASCII to hex and hex to ASCII converter class in JavaScript
- Finding the largest prime factor of a number in JavaScript
Advertisements