JavaScript | Check if a string is a valid hex color representation

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.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.