JavaScript: Check whether a given value is hexadecimal value or not
JavaScript validation with regular expression: Exercise-16 with Solution
Validate Hexadecimal
Write a JavaScript function to check whether a given value is a hexadecimal value or not.
Visual Presentation:

Sample Solution:
JavaScript Code:
function is_hexadecimal(str)
{
regexp = /^[0-9a-fA-F]+$/;
if (regexp.test(str))
{
return true;
}
else
{
return false;
}
}
console.log(is_hexadecimal("ffffff"));
console.log(is_hexadecimal("fz5500"));
Output:
true false
Flowchart:

Live Demo:
See the Pen javascript-regexp-exercise-16 by w3resource (@w3resource) on CodePen.
For more Practice: Solve these Related Problems:
- Write a JavaScript function that uses a regular expression to check if a string is a valid hexadecimal number (with or without 0x).
- Write a JavaScript function that validates a hexadecimal string by ensuring it contains only digits and letters A-F/a-f.
- Write a JavaScript function that trims the input and converts it to uppercase before validating it as a hexadecimal value.
- Write a JavaScript function that tests for both short and long hexadecimal formats and returns true if valid.
Improve this sample solution and post your code through Disqus.
Previous: Write a JavaScript function to check whether a given value is a social security number or not.
Next: Write a JavaScript function to check whether a given value is hexcolor value or not.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.