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

For some reason, code doesn't work: Bummer! Your regular expression matches CSS hexadecimal color values, but also match

Hi,

Can't make it work, can someone help me out?

app.js
// Type inside this function
function isValidHex(text) {
var hexRegEx = /#[a-f0-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>

Here's the task:

For this step of the challenge, write a regular expression that will match any hexadecimal string and store it in a variable named hexRegEx. The regex should be case-insensitive. Assume that the string will always be 7 characters. I assigned a regex to the var but it doesn't pass the test, what I'm doing wrong?

8 Answers

Made it work with the following example: var hexRegEx = /^#[a-f0-9]{6}$/i;

Cheers!

Rhys Kearns
Rhys Kearns
4,976 Points

You are passing text into the function but you are simply doing nothing with it - im not sure how you are verifying that text fits with your regex verification. Maybe you need to break it down and go step by step, tell me if you need anymore help :)

Here's the task:

For this step of the challenge, write a regular expression that will match any hexadecimal string and store it in a variable named hexRegEx. The regex should be case-insensitive. Assume that the string will always be 7 characters.

Have I misunderstood smth?

Then it says Bummer! The variable hexRegEx is not declared inside the isValidHex function.

Any thoughts?

Rhys Kearns
Rhys Kearns
4,976 Points

Yeah that was my bad ignore my last comment! I didnt check the challenge

Rhys Kearns
Rhys Kearns
4,976 Points

Bummer! Your regular expression matches CSS hexadecimal color values, but also matches other values. See if you can limit your regex to meet the requirements.

Is the error message, I haven't watched the video so im not quiet sure what you learnt before this challenge :)

Yes, but how to limit it? I checked my code using different online regex tools, like this one: https://regex101.com/ and it seems to work, so I'm a bit lost here.

Rhys Kearns
Rhys Kearns
4,976 Points

I actually can't figure out why it isnt working... Starting to annoy me now haha