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 trialChristopher Aporta
24,258 PointsWanted to share some additional functionality I added to this project :)
I decided to revisit this project to add a bit of additional functionality. I wanted to test for the presence of 'invalid' characters in the username field. I did so by using a Regular Expression (i.e. regex for short), and thought this would be a nice technique to share with my awesome Treehouse classmates that goes a bit beyond what's required in this lesson.
Here's the additional code:
//check if username is valid
var usernameValid = function () {
var str = $username.val();
return /^[a-zA-Z0-9]*$/.test(str);
};
var usernameEvent = function () {
//if valid
if (usernameValid()) {
//hide
$username.next().hide();
} else {
//show
$username.next().show();
}
};
//when user focuses on $username
$username.focus(usernameEvent).keyup(usernameEvent).keyup(confirmationEvent).keyup(enableSubmitEvent);
As you may have guessed, I added both a new span into the HTML for the username hint and usernameValid() to the canSubmit function.
You can manipulate the regex based on what you would choose to allow a user to include in their username.
Feel free to shoot any questions my way, or use this thread to expand even further on the functionality I've introduced here. Sharing is caring!
Liam Maclachlan
22,805 PointsMassive win, dude. Like it.
You can also include the underscore, too :)
^[a-zA-Z0-9_]*$
Christopher Aporta
24,258 PointsWell shucks. Thanks guys.
Christopher Aporta
24,258 PointsLiam,
I actually included that at first, but for brevity's sake, I wanted the hint to say "only alphanumeric characters allowed", so I excluded hyphens and underscores. But yeah, that works perfectly!
Liam Maclachlan
22,805 PointsAfter After a good night sleep, this has now inspired me to add this to a php database search function, to help order the results by specificity using the regex modifiers and multiple sql queries, before returning the results. Again, thanks for sharing man :)
Christopher Aporta
24,258 PointsLiam Maclachlan schweet.
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsAwesome use of regex, Chris!