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

need help with code challenge

Now, use the test() method to test the string in text against the regular expression you just created. Remember the test() method returns a true or false value. Return the result of testing the string from the isValidHex method.

```// Type inside this function function isValidHex(text) { const 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)"; } }); /``` Could someone help out with this challenge please?

I know I should use : text.test(hexRegEx) but that is as all.

app.js
// Type inside this function
function isValidHex(text) {
     const 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>
Charlie Gallentine
Charlie Gallentine
12,092 Points

I'm not quite sure, but try including your regex testing statement inside the function where

// Type inside this function

Similar to :

function isValidHex(text) {
     const hexRegEx = /^#[a-f0-9]{6}$/i;
     return text.test(hexRegEx);
}

That should cause the function to return the boolean value of calling the .test() method on "text".

Hope that helps, otherwise I'm sure someone will chime in soon with a better answer!

Brandon Benefield
Brandon Benefield
7,739 Points

Charlie Gallentine There can't be a better answer because that IS the answer.

PS
You either have two accounts or you share the same name with someone else on treehouse... spooky

Charlie Gallentine
Charlie Gallentine
12,092 Points

@Brandon Benefield Haha yeah, I had a Treehouse account last year but had to cancel it for time and budget reasons. When I came back, I forgot about the old one so now I get to be my own twin :)

1 Answer

// Type inside this function
function isValidHex(text) {
  var hexRegEx = /^#[a-fA-F0-9]{6}$/;
  return hexRegEx.test(text);
}

This code should do fine. It asks you to test a value and return a boolean. so it's: return (boolean value) (your regex).test(on value);

Mike Siwik
seal-mask
.a{fill-rule:evenodd;}techdegree
Mike Siwik
Full Stack JavaScript Techdegree Student 8,483 Points

In a previous video, it did not indicate that you could do

[a-f0-9]
I tried many different combinations like /^\(\d[a-f]\)\s\d[0-9]$/i.test(text);

And nothing worked. thank you for this