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

chris Nelson
Courses Plus Student 210 PointsCreate a simple validation form using jQuery
I am trying to create a simple validation form using jQuery. My code currently doesn't do anything, If someone could explain to me what I'm doing wrong I would really appreciate it!
$(function(){
$("#send").click(function(){
var errors = false;
var emailReg = "[a-zA-Z0-9\\.]+@[a-zA-Z0-9\\-\\_\\.]+\\.[a-zA-Z0-9]{3}";
//validate email
$(".errors").remove();
//refresh error messages on submit
if ($(!"#name").val() === ""){
$("#name").after( "<span class='errors'> Missing Name </span> ")
errors= true;
}
//validate name field has entry
if (!$( "#email" ).val() === ""){
$("#email").after("<span class='errors'> Missing Email </span>")
errors= true;
}
else if( !emailReg.test($("#email").val())){
$("#email").after( "<span class='errors'> Not a valid email </span> ")
errors = true;
//validate email field has entry and is a valid
}
if(errors === true){
return false;}
else {return true;}
});
});
//validate form
<!DOCTYPE html>
<html>
<head>
<title>Form validate</title>
<link href="css/style.css" rel='stylesheet'>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<h1>Get A Quote</h1>
<form action='contact.php' method="POST">
<p>*Name:</p><input id="name" type="text" name='name'>
<br>
<br>
<p>*Email:</p> <input id="email" type="text" name="email">
<br><br>
<p>Budget:<br></p> <input id="budget" type="text" name="budget">
<br><br>
<p>Describe your project:</p><textarea id="project" name="message" rows="6" cols="55"></textarea><br>
<br><br>
<input class='button send' type="submit" value="Send">
</form>
<h5>* Required fields</h5>
</body>
<script src=js/val.js></script>
</html>

Chyno Deluxe
16,936 Pointscan you provide your html as well.
1 Answer

LaVaughn Haynes
12,397 PointsThe code below would work. You might revisit that regular expression. There are also other changes you could make for performance but I don't want to change your code too much. One of them is that you can cache some of those dom queries that you make repeatedly such as $('#email')
(function($){
$('form').on('submit', function(){
var errors = false;
var emailReg = /^([a-zA-Z0-9\\.]+)@([a-zA-Z0-9\\-\\_\\.]+)\.([a-zA-Z0-9]+)$/i;
//validate email
$(".errors").remove();
//refresh error messages on submit
if($("#name").val() === ""){
$("#name").after( "<span class='errors'> Missing Name </span> ");
errors = true;
}
//validate name field has entry
if($("#email").val() === ""){
$("#email").after("<span class='errors'> Missing Email </span>");
errors = true;
}else if(!emailReg.test($("#email").val())){
$("#email").after( "<span class='errors'> Not a valid email </span> ")
errors = true;
}
return !errors;
});
})(jQuery);
Also, in your html, put your script tag before the closing body tag.
<script src="js/val.js"></script>
</body>

chris Nelson
Courses Plus Student 210 PointsThank you for the help and the advice! I really appreciate it!

LaVaughn Haynes
12,397 PointsNo problem. Chyno has an awesome form validation pen that you might want to check out. If you are just learning javaScript it might be a little advanced for you for now, but maybe bookmark it and check it out again after you finish more of the javaScript track.

Chyno Deluxe
16,936 PointsLaVaughn Haynes =) Thanks for the plug. haha.
LaVaughn Haynes
12,397 PointsLaVaughn Haynes
12,397 PointsI fixed your post so that your code displays correctly. I have to get ready for work now but I'll take a look at helping you find a solution for this tomorrow if no solutions are posted before.