---
title: "Validate a hexadecimal color code"  
description: "Validate a hexadecimal color code"  
author: "Pravesh Singh"  
published: 2013-03-09  
updated: 2013-03-09  
canonical: https://www.mindstick.com/forum/636/validate-a-hexadecimal-color-code  
category: "javascript"  
tags: ["javascript"]  
reading_time: 1 minute  

---

# Validate a hexadecimal color code

Hi All!

Please, I'm looking for a [validation](https://www.mindstick.com/articles/12234/validation-using-data-annotation-using-entity-framework) able to [validate](https://www.mindstick.com/forum/1490/what-is-better-solution-for-validate-form-textboxs-value) a [CSS](https://www.mindstick.com/forum/514/background-gradient-ie7-css-problem) [color](https://www.mindstick.com/articles/77/how-to-split-form-background-color-in-c-sharp) [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) ([e.g](https://www.mindstick.com/forum/157370/how-to-swap-neighbor-characters-in-string-e-g-string-demo-would-be-edom): #ffffff).

[Conditions](https://www.mindstick.com/blog/394/javascript-if-else) (you may know) are:\
Begin => #;\
strlen => 7 char with include #;\
[a-fA-F0-9] in any combination or [order](https://www.mindstick.com/articles/12276/how-timely-order-deliveries-can-improve-customer-experience);

Thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)!

## Replies

### Reply by AVADHESH PATEL

Hi Pravesh!

You could try following line of code

```
<script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.maskedinput.js" type="text/javascript"></script>
<script type="text/javascript">
    jQuery(function ($) {
        $.mask.definitions['h'] = "[A-Fa-f0-9]";
        $("#hexacolor").mask("#hhhhhh");
    });
</script><input id="hexacolor" type="text" />
```

Note:\
"jquery-1.7.1.min.js" provided by MVC application\
"jquery.maskedinput.js" you can download from following link

[https://raw.github.com/digitalBush/jquery.maskedinput/1.3.1/dist/jquery.maskedinput.min.js](https://raw.github.com/digitalBush/jquery.maskedinput/1.3.1/dist/jquery.maskedinput.min.js)


---

Original Source: https://www.mindstick.com/forum/636/validate-a-hexadecimal-color-code

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
