Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Regular Expressions in JavaScript Validating a Form Form Validation

Everyone look at my RegEx, help. It says it's valid on Hex colors, but says I need to help it find non valid characters.

// Type inside this function

function isValidHex(text) {

/* RIGHT HERE BELOW, I have a regex that checks CSS Hex colors. I guess in someway it is possible for people to get around my pattern and enter non-valid Hex/CSS colors that will get past my Regular Expression pattern. I have been using RegExpal and can't see this. Maybe some insight because the Treehouse quiz will not let me continue without solving this . */ var hexRegEx = /#[a-z0-9]{6}/i; }

const hex = document.getElementById("hex"); const body = document.getElementsByTagName("body")[0];

hex.addEventListener("input", e => { const text = e.target.value; const valid = isValidHex(text); if (valid) { body.style.backgroundColor = "rgb(176, 208, 168)"; } else { body.style.backgroundColor = "rgb(189, 86, 86)"; } });

app.js
// Type inside this function
function isValidHex(text) {
var hexRegEx = /#[a-z0-9]{6}/i;
}

const hex = document.getElementById("hex");
const body = document.getElementsByTagName("body")[0];

hex.addEventListener("input", e => {
  const text = e.target.value;
  const valid = isValidHex(text);
  if (valid) {
    body.style.backgroundColor = "rgb(176, 208, 168)";
  } else {
    body.style.backgroundColor = "rgb(189, 86, 86)";
  }
});
index.html
<!DOCTYPE html>
<html>

<head>
    <title>DOM Manipulation</title>
</head>
<link rel="stylesheet" href="style.css" />

<body>
    <div id="content">
        <p>Enter a valid hex value below to make the screen turn green.</p>
        <input type="text" id="hex">
    </div>
    <script src="app.js"></script>
</body>

</html>

Hey Cody!

I believe that you messaged me from my website, but unfortunately the email address you included does not seem to be your personal one.

If you are interested in asking me some question, please message me again, but include a personal email address that I can reach you at :D

2 Answers

function isValidHex(text) {

const hexRegEx =/^#[a-f0-9]{6}$/i;

}

Seth Kroger
Seth Kroger
56,414 Points

Hexadecimal uses 16 digits. 0 to 9 are used to cover the first 10, but letters for the next 6. Since there are only 6 digits represented by letters you don't use the whole 26 letters from a-z but only the first 6, a-f.