Skip to content
Related Articles

Related Articles

Improve Article
JavaScript | Check if a string is a valid hex color representation
  • Last Updated : 19 Jul, 2019

Given a hexCode, The task is to know whether the given hexCode is valid or not using JavaScript. we’re going to discuss few techniques.

Approach:

  • Valid hexcode contains the alphabets(A-F) and integers(0-9) and is of length 3 or 6.
  • A RegExp is used to check whether the alphabets ranging between A and F, Integers from 0-9 and is of length = 3 or 6.

Example 1: In this example, the validity of the hexcode is checked by Regular Expression.




<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        JavaScript 
      | Check if a string is a valid hex color representation.
    </title>
</head>
  
<body style="text-align:center;" 
      id="body">
    <h1 style="color:green;">  
            GeeksForGeeks
        </h1>
    <p id="GFG_UP" 
       style="font-size: 19px;
              font-weight: bold;">
    </p>
    <button onclick="GFG_Fun()">
        click here
    </button>
    <p id="GFG_DOWN" 
       style="color: green;
              font-size: 24px;
              font-weight: bold;">
    </p>
    <script>
        var el_up = 
            document.getElementById("GFG_UP");
        var el_down = 
            document.getElementById("GFG_DOWN");
        var colorCode = '#zabbcc';
        el_up.innerHTML = "Click on the button to check whether '"
                          + colorCode + 
                          "' hexCode is valid or not.";
        
        var RegExp = /^#[0-9A-F]{6}$/i;
  
        function GFG_Fun() {
            el_down.innerHTML = RegExp.test(colorCode);
        }
    </script>
</body>
  
</html>

Output:

  • Before clicking on the button:
  • After clicking on the button:

Example 2: This example does much more advanced checking than the previous example. In this example also, the validity of the hexcode is checked.




<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        JavaScript |
      Check if a string is a valid hex color representation.
    </title>
</head>
  
<body style="text-align:center;"
      id="body">
    <h1 style="color:green;">  
            GeeksForGeeks
        </h1>
    <p id="GFG_UP" 
       style="font-size: 19px;
              font-weight: bold;">
    </p>
    <button onclick="GFG_Fun()">
        click here
    </button>
    <p id="GFG_DOWN" 
       style="color: green; 
              font-size: 24px; 
              font-weight: bold;">
    </p>
    <script>
        var el_up = 
            document.getElementById("GFG_UP");
        var el_down = 
            document.getElementById("GFG_DOWN");
        var colorCode = '#aabbcc';
        el_up.innerHTML = "Click on the button to check"+
          " whether given hexCode is valid or not.";
        var RegExp = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i;
  
        function GFG_Fun() {
            el_down.innerHTML = RegExp.test(colorCode);
        }
    </script>
</body>
  
</html>

Output:

  • Before clicking on the button:
  • After clicking on the button:



My Personal Notes arrow_drop_up
Recommended Articles
Page :