
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 Articles
- Check for Valid hex code - JavaScript
- Checking the validity of parentheses in JavaScript
- Checking validity of equations in JavaScript
- ASCII to hex and hex to ASCII converter class in JavaScript
- Validity of a Psychological Test
- Finding the length of a JavaScript object
- Generating random hex color in JavaScript
- Finding the number of words in a string JavaScript
- Types of Validity
- Finding the inclination of arrays in JavaScript
- Finding the balance of brackets in JavaScript
- Finding the ASCII score of a string - JavaScript
- Finding the largest prime factor of a number in JavaScript
- Setting validity of a user in SAP HANA
- Finding place value of a number in JavaScript

Advertisements