Home > DeveloperSection > Forums > Validate a hexadecimal color code
Pravesh Singh

Total Post:411

Points:2881
Posted on    March-09-2013 4:55 AM

 JavaScript JavaScript 
Ratings:


 1 Reply(s)
 1129  View(s)
Rate this:

Hi All!

Please, I'm looking for a validation able to validate a CSS color code (e.g: #ffffff).

Conditions (you may know) are:
Begin => #;
strlen => 7 char with include #;
[a-fA-F0-9] in any combination or order;

Thanks in advance!



AVADHESH PATEL

Total Post:604

Points:4228
Posted on    March-09-2013 8:29 AM

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


Don't want to miss updates? Please click the below button!

Follow MindStick