jquery validate does not validate!
Hello there!!
I have to do a form with 3 fiedsets using jquery validation.
I have to do:
1st fieldset: Your details
Title (required)
First Name (required)
Surname (required)
Email address (valid email -containing @)
Telephone number (valid UK number)
2nd fieldset: Password
Password (required)
Confirm password (required and the same string as above)
3rd fieldset: Address details
Address 1 (required)
Address 2
Address 3
Town (required)
Postcode (required)
The point is that nothing works :( I know basics of programming and I think it is too much for me, because I can not understand anything I read about it....
The big problem is: although now it even validate the full form, before it worked in this way, but I have to validate each fieldset.
I've tried the ignore method.... and I think I'm using it wrong. I though as well it could be easier to do it changing each fieldset for forms (so, I would have 3 forms) and then use a jquery function to call the first and the second form in the third form to submit all of it.
Can someone give me a hand???? I really really need some help, I've been stock in this 2 days and nothing works yet :/
Here is the html
-----------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<title>Multi Form</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="css/multiForm.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- multistep form -->
<form id="msform">
<!-- progressbar -->
<ul id="progressbar">
<li class="active">Your details</li>
<li>Password</li>
<li>Address details</li>
</ul>
<!-- fieldsets -->
<fieldset id="fieldset1">
<h2 class="fs-title">Your details</h2>
<h3 class="fs-subtitle">This is step 1</h3>
<input type="text" name="title" placeholder="Title"/>
<input type="text" name="fname" placeholder="First Name" />
<input type="text" name="sname" placeholder="Surname" />
<input type="text" name="email" placeholder="Email address" />
<input type="text" name="phone" placeholder="Phone number" />
<textarea name="address" placeholder="Address"></textarea>
<input type="button" name="next" class="next action-button" value="Next" />
</fieldset>
<fieldset id="fieldset2">
<h2 class="fs-title">Password</h2>
<h3 class="fs-subtitle">It is save with us</h3>
<input type="password" name="pass" placeholder="Password" class="required" />
<input type="password" name="cpass" placeholder="Confirm Password" />
<input type="button" name="previous" class="previous action-button" value="Previous" />
<input type="button" name="next" class="next action-button" value="Next" />
</fieldset>
<fieldset id="fieldset3">
<h2 class="fs-title">Address Details</h2>
<h3 class="fs-subtitle">This is the last step</h3>
<textarea name="address" placeholder="Address 1"></textarea>
<textarea name="address" placeholder="Address 2"></textarea>
<textarea name="address" placeholder="Address 3"></textarea>
<input type="text" name="town" placeholder="Town" />
<input type="text" name="postcode" placeholder="Postcode" />
<input type="button" name="previous" class="previous action-button" value="Previous" />
<input type="submit" name="submit" class="submit action-button" value="Submit" />
</fieldset>
</form>
<script src="js/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="js/multiForm.js" type="text/javascript"></script>
<script src="js/jquery.easing.js" type="text/javascript"></script>
<script src="js/jquery.validate.min.js" type="text/javascript"></script>
<script>
$.validator.setDefaults({
submitHandler: function() {
alert("submitted!");
}
});
$(document).ready(function() {
$('#msform').validate();
});
$("#myform").validate({ignore:".ignore"});
$("#fieldset1").click(function(){
$("input[name=name]").removeClass("ignore");
$("input[name=something_else]").addClass("ignore"); //this input won't be validated.
if(!$("#myform").valid())
// do not proceed to page 2
});
$("#submit").click(function(){
$(this).find("input").removeClass("ignore"); // validate all input
if(!$(this).valid())
return false;
});
</script>
</body>
</html>
Here the js
---------------------------------------------------------
var current_fs, next_fs, previous_fs; //fieldsets
var left, opacity, scale; //fieldset properties which we will animate
$(".next").click(function(){
$(document).ready(function() {
$('#fielset1').validate();
});
current_fs = $(this).parent();
next_fs = $(this).parent().next();
//activate next step on progressbar using the index of next_fs
$("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");
//show the next fieldset
next_fs.show();
current_fs.hide();
//hide the current fieldset with style
});
$(".previous").click(function(){
current_fs = $(this).parent();
previous_fs = $(this).parent().prev();
//de-activate current step on progressbar
$("#progressbar li").eq($("fieldset").index(current_fs)).removeClass("active");
//show the previous fieldset
previous_fs.show();
current_fs.hide();
//hide the current fieldset with style
});
$("#msform").validate({
/* */
errorPlacement: function(error, element)
{
if( element.closest("#fieldset1").length ) {
/* special handling */
} else {
/* generic handling */
error.insertAfter(element);
}
}
});
Here the css
-----------------------------------------------
/*custom font*/
@import url(
http://fonts.googleapis.com/css?family=Montserrat);
/*basic reset*/
* {margin: 0; padding: 0;}
html {
height: 100%;
/*Image only BG fallback*/
background: url('
http://thecodeplayer.com/uploads/media/gs.png');
/*background = gradient + image pattern combo*/
background:
linear-gradient(rgba(196, 102, 0, 0.2), rgba(155, 89, 182, 0.2)),
url('
http://thecodeplayer.com/uploads/media/gs.png');
}
body {
font-family: montserrat, arial, verdana;
}
/*form styles*/
#msform {
width: 400px;
margin: 50px auto;
text-align: center;
position: relative;
}
#msform fieldset {
background: white;
border: 0 none;
border-radius: 3px;
box-shadow: 0 0 15px 1px rgba(0, 0, 0, 0.4);
padding: 20px 30px;
box-sizing: border-box;
width: 80%;
margin: 0 10%;
/*stacking fieldsets above each other*/
position: absolute;
}
/*Hide all except first fieldset*/
#msform fieldset:not(:first-of-type) {
display: none;
}
/*inputs*/
#msform input, #msform textarea {
padding: 15px;
border: 1px solid #ccc;
border-radius: 3px;
margin-bottom: 10px;
width: 100%;
box-sizing: border-box;
font-family: montserrat;
color: #2C3E50;
font-size: 13px;
}
/*buttons*/
#msform .action-button {
width: 100px;
background: #27AE60;
font-weight: bold;
color: white;
border: 0 none;
border-radius: 1px;
cursor: pointer;
padding: 10px 5px;
margin: 10px 5px;
}
#msform .action-button:hover, #msform .action-button:focus {
box-shadow: 0 0 0 2px white, 0 0 0 3px #27AE60;
}
/*headings*/
.fs-title {
font-size: 15px;
text-transform: uppercase;
color: #2C3E50;
margin-bottom: 10px;
}
.fs-subtitle {
font-weight: normal;
font-size: 13px;
color: #666;
margin-bottom: 20px;
}
/*progressbar*/
#progressbar {
margin-bottom: 30px;
overflow: hidden;
/*CSS counters to number the steps*/
counter-reset: step;
}
#progressbar li {
list-style-type: none;
color: white;
text-transform: uppercase;
font-size: 9px;
width: 33.33%;
float: left;
position: relative;
}
#progressbar li:before {
content: counter(step);
counter-increment: step;
width: 20px;
line-height: 20px;
display: block;
font-size: 10px;
color: #333;
background: white;
border-radius: 3px;
margin: 0 auto 5px auto;
}
/*progressbar connectors*/
#progressbar li:after {
content: '';
width: 100%;
height: 2px;
background: white;
position: absolute;
left: -50%;
top: 9px;
z-index: -1; /*put it behind the numbers*/
}
#progressbar li:first-child:after {
/*connector not needed before the first step*/
content: none;
}
/*marking active/completed steps green*/
/*The number of the step and the connector before it = green*/
#progressbar li.active:before, #progressbar li.active:after{
background: #27AE60;
color: white;
}
p.s: I say again, I'm very new in programming, I would need an easy explanation.
thanks a lot!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!