- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Check for Valid hex code - 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
Following is the code −
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
Following is the output in the console −
false true
- Related Articles
- Finding the validity of a hex code in JavaScript
- Check valid date format in JavaScript?
- C++ code to check pile count is valid from second day
- How to check whether a JavaScript date is valid?
- What characters are valid for JavaScript variable names?
- C++ code to check query for 0 sum
- JavaScript code for recursive Fibonacci series
- ASCII to hex and hex to ASCII converter class in JavaScript
- PHP – Check if strings are valid for the specified encoding using mb_check_encoding()
- Simplest code for array intersection in JavaScript?
- Valid triangle edges - JavaScript
- Is wrapping a div inside an anchor tag valid code?
- Generating random hex color in JavaScript
- 8085 Program to check for two out of five code
- Check whether right angled triangle is valid or not for large sides in Python

Advertisements