How to validate a form with jQuery











up vote
1
down vote

favorite












I've been stuck trying to validate a form using juery, I would like to make sure all the fields have been filled as well as the checkboxes/radio buttons. Here is my code (kind of a mess but I'm pretty new to coding and had to learn by myself)



Any answers on how to fix it would be thoroughly appreciated, thanks.






<script>
//Creating the validation function
$(document).ready(function() {
$('form[id="checkout"]').validate({
//Setting up the requirements for form
rules: {
//Check if name is entered
firstname: 'required',
lastname: 'required',

//Check if genders is selected
genders: 'required',

//Check if birthday is entered
bday: 'required',

messages: {
firstname: 'Please enter your first name',
lastname: 'Please enter your last name',
genders: 'Please select your gender',
bday: 'Please enter your birthday',
checkbox: 'Please check at least one box'
},

//Check if at least one checkbox has been filled

//If everything meets the requirements, submit the form
submitHandler: function(form) {
form.submit();
}
};
});
});


function checkboxFunction() {
var checkBox = document.getElementById("checkbox");
var text = document.getElementById("text");
if (checkBox.checked == true){
text.style.display = "none";
} else {
text.style.display = "checkbox";
}
}

</script>

body {
font-family: 'Roboto', sans-serif;
background-color: #E4DFDA;
}

h1 {
letter-spacing: 3px;
font-size: 30pt;
background-color: #4062BB;
height: 110px;
padding-top: 45px;
color: white;
top: 0;
text-align: center;
}

h2 {
font-weight: 200;
}

h3 {
font-weight: 100;
}

.container {
width:100%;
text-align: center;
margin: auto;
}

.form-outline {
width: 35%;
margin: auto;
text-align: left;
}

.bday {
padding: 10px 25px;
border-radius: 5px;
}

.url-form {
padding: 10px 35px;
text-align: left;
}

form {
text-align: left;
}

.bottom-form {
text-align: center;
margin:auto;
}

.required:after {
content:" *";
color: red;
}

.button {
padding: 10px 25px;
color: white;
background-color: #4062BB;
font-size: 14pt;
font-weight: 100;
border-radius: 5px;
border: 3px solid #4062BB;
}

.button:hover {
color: #4062BB;
background-color: transparent;
}

.required:after { content:" *"; }


.error {
color: red;
margin-left: 5px;
}

label.error {
display: inline;
}



#footer {
margin-top: 100px;
background-color: grey;
padding-top: 45px;
padding-bottom: 45px;
color: white;
bottom: 0;
text-align: center;

}

<body>

<h1>Checkout</h1>
<br>
<div class="container">
<h2>Almost done! All we need now is a little bit more about you.</h2>
<h3><em>Please fill all the fields marked with <strong style="color:red;">*</strong> before submitting.</em></h3>
<br>
<p>_____________________</p>
<br>
<div class="form-outline">
<!--Creating the form-->
<form action="https://formspree.io/prometheus111222333@gmail.com" method="POST" name="CheckoutForm" onsubmit="FormValidate()" id="checkout">
<h3 class="required">What is your name?</h3>
<!--Enter name-->
<input type="text" name="firstname" id="firstname" placeholder="First name" style="padding:10px; border-radius:3px;" for="firstname">

<input type="text" name="lastname" id="lastname" placeholder="Last name" style="padding:10px; border-radius:3px;" for="lastname">
<br><br>
<!--Display client ID (can't be modified)-->
<em>Client ID</em><br><br>
<input type="text" name="id" value="#1322247" style="text-align:center; padding:10px; border-radius:3px;" disabled>
<br><br>

<!--Select gender-->
<h3 class="required" id="genders" for="genders">What is your gender?</h3>
<input type="radio" name="gender" id="male" value="male"> Male<br>
<input type="radio" name="gender" id="female" value="female"> Female<br>
<input type="radio" name="gender" id="other" value="other"> Other
<br><br>

<h3 class="required">When is your birthday?</h3>
<!--Enter Birthday-->
<input type="date" name="bday" id="bday" class="bday" for="bday">
<br><br>

<h3 class="required">Do you own any of the following items?</h3>
<h4>(check none if you do not)</h4>
<!--Checkbox-->
<input type="checkbox" name="vehicule1" id="vehicule1" value="Bicycle"> Bicycle<br>
<input type="checkbox" name="vehicule2" id="vehicule2" value="Car"> Car <br>
<input type="checkbox" name="vehicule3" id="vehicule3" value="Motorcycle"> Motorcycle <br>
<input type="checkbox" name="vehicule4" id="vehicule4" value="Tractor"> Tractor <br>
<input type="checkbox" name="vehicule5" id="vehicule5" value="None"> None
<br>


<div class="bottom-form">
<p>_____________________</p>
<br>
<h2>Now, what can we do for you?</h2>

<h3>Enter your website's url to get promoted on ours!</h3>
<!--Enter website url-->
<input type="url" name="websiteUrl" id="websiteUrl" class="url-form"> <br><br>


<h3>All done? Click submit!</h3>
<input class="button" type="button" onclick="function()" value="Submit" >
</div>


</form>
</div>
</div>

<!--Adding the footer-->
<h2 id="footer">Thank you for your trust & fidelity</h2>

</body>












share|improve this question




















  • 1




    You should use something like github.com/jquery-validation/jquery-validation
    – Christopher Dosin
    Nov 11 at 4:41















up vote
1
down vote

favorite












I've been stuck trying to validate a form using juery, I would like to make sure all the fields have been filled as well as the checkboxes/radio buttons. Here is my code (kind of a mess but I'm pretty new to coding and had to learn by myself)



Any answers on how to fix it would be thoroughly appreciated, thanks.






<script>
//Creating the validation function
$(document).ready(function() {
$('form[id="checkout"]').validate({
//Setting up the requirements for form
rules: {
//Check if name is entered
firstname: 'required',
lastname: 'required',

//Check if genders is selected
genders: 'required',

//Check if birthday is entered
bday: 'required',

messages: {
firstname: 'Please enter your first name',
lastname: 'Please enter your last name',
genders: 'Please select your gender',
bday: 'Please enter your birthday',
checkbox: 'Please check at least one box'
},

//Check if at least one checkbox has been filled

//If everything meets the requirements, submit the form
submitHandler: function(form) {
form.submit();
}
};
});
});


function checkboxFunction() {
var checkBox = document.getElementById("checkbox");
var text = document.getElementById("text");
if (checkBox.checked == true){
text.style.display = "none";
} else {
text.style.display = "checkbox";
}
}

</script>

body {
font-family: 'Roboto', sans-serif;
background-color: #E4DFDA;
}

h1 {
letter-spacing: 3px;
font-size: 30pt;
background-color: #4062BB;
height: 110px;
padding-top: 45px;
color: white;
top: 0;
text-align: center;
}

h2 {
font-weight: 200;
}

h3 {
font-weight: 100;
}

.container {
width:100%;
text-align: center;
margin: auto;
}

.form-outline {
width: 35%;
margin: auto;
text-align: left;
}

.bday {
padding: 10px 25px;
border-radius: 5px;
}

.url-form {
padding: 10px 35px;
text-align: left;
}

form {
text-align: left;
}

.bottom-form {
text-align: center;
margin:auto;
}

.required:after {
content:" *";
color: red;
}

.button {
padding: 10px 25px;
color: white;
background-color: #4062BB;
font-size: 14pt;
font-weight: 100;
border-radius: 5px;
border: 3px solid #4062BB;
}

.button:hover {
color: #4062BB;
background-color: transparent;
}

.required:after { content:" *"; }


.error {
color: red;
margin-left: 5px;
}

label.error {
display: inline;
}



#footer {
margin-top: 100px;
background-color: grey;
padding-top: 45px;
padding-bottom: 45px;
color: white;
bottom: 0;
text-align: center;

}

<body>

<h1>Checkout</h1>
<br>
<div class="container">
<h2>Almost done! All we need now is a little bit more about you.</h2>
<h3><em>Please fill all the fields marked with <strong style="color:red;">*</strong> before submitting.</em></h3>
<br>
<p>_____________________</p>
<br>
<div class="form-outline">
<!--Creating the form-->
<form action="https://formspree.io/prometheus111222333@gmail.com" method="POST" name="CheckoutForm" onsubmit="FormValidate()" id="checkout">
<h3 class="required">What is your name?</h3>
<!--Enter name-->
<input type="text" name="firstname" id="firstname" placeholder="First name" style="padding:10px; border-radius:3px;" for="firstname">

<input type="text" name="lastname" id="lastname" placeholder="Last name" style="padding:10px; border-radius:3px;" for="lastname">
<br><br>
<!--Display client ID (can't be modified)-->
<em>Client ID</em><br><br>
<input type="text" name="id" value="#1322247" style="text-align:center; padding:10px; border-radius:3px;" disabled>
<br><br>

<!--Select gender-->
<h3 class="required" id="genders" for="genders">What is your gender?</h3>
<input type="radio" name="gender" id="male" value="male"> Male<br>
<input type="radio" name="gender" id="female" value="female"> Female<br>
<input type="radio" name="gender" id="other" value="other"> Other
<br><br>

<h3 class="required">When is your birthday?</h3>
<!--Enter Birthday-->
<input type="date" name="bday" id="bday" class="bday" for="bday">
<br><br>

<h3 class="required">Do you own any of the following items?</h3>
<h4>(check none if you do not)</h4>
<!--Checkbox-->
<input type="checkbox" name="vehicule1" id="vehicule1" value="Bicycle"> Bicycle<br>
<input type="checkbox" name="vehicule2" id="vehicule2" value="Car"> Car <br>
<input type="checkbox" name="vehicule3" id="vehicule3" value="Motorcycle"> Motorcycle <br>
<input type="checkbox" name="vehicule4" id="vehicule4" value="Tractor"> Tractor <br>
<input type="checkbox" name="vehicule5" id="vehicule5" value="None"> None
<br>


<div class="bottom-form">
<p>_____________________</p>
<br>
<h2>Now, what can we do for you?</h2>

<h3>Enter your website's url to get promoted on ours!</h3>
<!--Enter website url-->
<input type="url" name="websiteUrl" id="websiteUrl" class="url-form"> <br><br>


<h3>All done? Click submit!</h3>
<input class="button" type="button" onclick="function()" value="Submit" >
</div>


</form>
</div>
</div>

<!--Adding the footer-->
<h2 id="footer">Thank you for your trust & fidelity</h2>

</body>












share|improve this question




















  • 1




    You should use something like github.com/jquery-validation/jquery-validation
    – Christopher Dosin
    Nov 11 at 4:41













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I've been stuck trying to validate a form using juery, I would like to make sure all the fields have been filled as well as the checkboxes/radio buttons. Here is my code (kind of a mess but I'm pretty new to coding and had to learn by myself)



Any answers on how to fix it would be thoroughly appreciated, thanks.






<script>
//Creating the validation function
$(document).ready(function() {
$('form[id="checkout"]').validate({
//Setting up the requirements for form
rules: {
//Check if name is entered
firstname: 'required',
lastname: 'required',

//Check if genders is selected
genders: 'required',

//Check if birthday is entered
bday: 'required',

messages: {
firstname: 'Please enter your first name',
lastname: 'Please enter your last name',
genders: 'Please select your gender',
bday: 'Please enter your birthday',
checkbox: 'Please check at least one box'
},

//Check if at least one checkbox has been filled

//If everything meets the requirements, submit the form
submitHandler: function(form) {
form.submit();
}
};
});
});


function checkboxFunction() {
var checkBox = document.getElementById("checkbox");
var text = document.getElementById("text");
if (checkBox.checked == true){
text.style.display = "none";
} else {
text.style.display = "checkbox";
}
}

</script>

body {
font-family: 'Roboto', sans-serif;
background-color: #E4DFDA;
}

h1 {
letter-spacing: 3px;
font-size: 30pt;
background-color: #4062BB;
height: 110px;
padding-top: 45px;
color: white;
top: 0;
text-align: center;
}

h2 {
font-weight: 200;
}

h3 {
font-weight: 100;
}

.container {
width:100%;
text-align: center;
margin: auto;
}

.form-outline {
width: 35%;
margin: auto;
text-align: left;
}

.bday {
padding: 10px 25px;
border-radius: 5px;
}

.url-form {
padding: 10px 35px;
text-align: left;
}

form {
text-align: left;
}

.bottom-form {
text-align: center;
margin:auto;
}

.required:after {
content:" *";
color: red;
}

.button {
padding: 10px 25px;
color: white;
background-color: #4062BB;
font-size: 14pt;
font-weight: 100;
border-radius: 5px;
border: 3px solid #4062BB;
}

.button:hover {
color: #4062BB;
background-color: transparent;
}

.required:after { content:" *"; }


.error {
color: red;
margin-left: 5px;
}

label.error {
display: inline;
}



#footer {
margin-top: 100px;
background-color: grey;
padding-top: 45px;
padding-bottom: 45px;
color: white;
bottom: 0;
text-align: center;

}

<body>

<h1>Checkout</h1>
<br>
<div class="container">
<h2>Almost done! All we need now is a little bit more about you.</h2>
<h3><em>Please fill all the fields marked with <strong style="color:red;">*</strong> before submitting.</em></h3>
<br>
<p>_____________________</p>
<br>
<div class="form-outline">
<!--Creating the form-->
<form action="https://formspree.io/prometheus111222333@gmail.com" method="POST" name="CheckoutForm" onsubmit="FormValidate()" id="checkout">
<h3 class="required">What is your name?</h3>
<!--Enter name-->
<input type="text" name="firstname" id="firstname" placeholder="First name" style="padding:10px; border-radius:3px;" for="firstname">

<input type="text" name="lastname" id="lastname" placeholder="Last name" style="padding:10px; border-radius:3px;" for="lastname">
<br><br>
<!--Display client ID (can't be modified)-->
<em>Client ID</em><br><br>
<input type="text" name="id" value="#1322247" style="text-align:center; padding:10px; border-radius:3px;" disabled>
<br><br>

<!--Select gender-->
<h3 class="required" id="genders" for="genders">What is your gender?</h3>
<input type="radio" name="gender" id="male" value="male"> Male<br>
<input type="radio" name="gender" id="female" value="female"> Female<br>
<input type="radio" name="gender" id="other" value="other"> Other
<br><br>

<h3 class="required">When is your birthday?</h3>
<!--Enter Birthday-->
<input type="date" name="bday" id="bday" class="bday" for="bday">
<br><br>

<h3 class="required">Do you own any of the following items?</h3>
<h4>(check none if you do not)</h4>
<!--Checkbox-->
<input type="checkbox" name="vehicule1" id="vehicule1" value="Bicycle"> Bicycle<br>
<input type="checkbox" name="vehicule2" id="vehicule2" value="Car"> Car <br>
<input type="checkbox" name="vehicule3" id="vehicule3" value="Motorcycle"> Motorcycle <br>
<input type="checkbox" name="vehicule4" id="vehicule4" value="Tractor"> Tractor <br>
<input type="checkbox" name="vehicule5" id="vehicule5" value="None"> None
<br>


<div class="bottom-form">
<p>_____________________</p>
<br>
<h2>Now, what can we do for you?</h2>

<h3>Enter your website's url to get promoted on ours!</h3>
<!--Enter website url-->
<input type="url" name="websiteUrl" id="websiteUrl" class="url-form"> <br><br>


<h3>All done? Click submit!</h3>
<input class="button" type="button" onclick="function()" value="Submit" >
</div>


</form>
</div>
</div>

<!--Adding the footer-->
<h2 id="footer">Thank you for your trust & fidelity</h2>

</body>












share|improve this question















I've been stuck trying to validate a form using juery, I would like to make sure all the fields have been filled as well as the checkboxes/radio buttons. Here is my code (kind of a mess but I'm pretty new to coding and had to learn by myself)



Any answers on how to fix it would be thoroughly appreciated, thanks.






<script>
//Creating the validation function
$(document).ready(function() {
$('form[id="checkout"]').validate({
//Setting up the requirements for form
rules: {
//Check if name is entered
firstname: 'required',
lastname: 'required',

//Check if genders is selected
genders: 'required',

//Check if birthday is entered
bday: 'required',

messages: {
firstname: 'Please enter your first name',
lastname: 'Please enter your last name',
genders: 'Please select your gender',
bday: 'Please enter your birthday',
checkbox: 'Please check at least one box'
},

//Check if at least one checkbox has been filled

//If everything meets the requirements, submit the form
submitHandler: function(form) {
form.submit();
}
};
});
});


function checkboxFunction() {
var checkBox = document.getElementById("checkbox");
var text = document.getElementById("text");
if (checkBox.checked == true){
text.style.display = "none";
} else {
text.style.display = "checkbox";
}
}

</script>

body {
font-family: 'Roboto', sans-serif;
background-color: #E4DFDA;
}

h1 {
letter-spacing: 3px;
font-size: 30pt;
background-color: #4062BB;
height: 110px;
padding-top: 45px;
color: white;
top: 0;
text-align: center;
}

h2 {
font-weight: 200;
}

h3 {
font-weight: 100;
}

.container {
width:100%;
text-align: center;
margin: auto;
}

.form-outline {
width: 35%;
margin: auto;
text-align: left;
}

.bday {
padding: 10px 25px;
border-radius: 5px;
}

.url-form {
padding: 10px 35px;
text-align: left;
}

form {
text-align: left;
}

.bottom-form {
text-align: center;
margin:auto;
}

.required:after {
content:" *";
color: red;
}

.button {
padding: 10px 25px;
color: white;
background-color: #4062BB;
font-size: 14pt;
font-weight: 100;
border-radius: 5px;
border: 3px solid #4062BB;
}

.button:hover {
color: #4062BB;
background-color: transparent;
}

.required:after { content:" *"; }


.error {
color: red;
margin-left: 5px;
}

label.error {
display: inline;
}



#footer {
margin-top: 100px;
background-color: grey;
padding-top: 45px;
padding-bottom: 45px;
color: white;
bottom: 0;
text-align: center;

}

<body>

<h1>Checkout</h1>
<br>
<div class="container">
<h2>Almost done! All we need now is a little bit more about you.</h2>
<h3><em>Please fill all the fields marked with <strong style="color:red;">*</strong> before submitting.</em></h3>
<br>
<p>_____________________</p>
<br>
<div class="form-outline">
<!--Creating the form-->
<form action="https://formspree.io/prometheus111222333@gmail.com" method="POST" name="CheckoutForm" onsubmit="FormValidate()" id="checkout">
<h3 class="required">What is your name?</h3>
<!--Enter name-->
<input type="text" name="firstname" id="firstname" placeholder="First name" style="padding:10px; border-radius:3px;" for="firstname">

<input type="text" name="lastname" id="lastname" placeholder="Last name" style="padding:10px; border-radius:3px;" for="lastname">
<br><br>
<!--Display client ID (can't be modified)-->
<em>Client ID</em><br><br>
<input type="text" name="id" value="#1322247" style="text-align:center; padding:10px; border-radius:3px;" disabled>
<br><br>

<!--Select gender-->
<h3 class="required" id="genders" for="genders">What is your gender?</h3>
<input type="radio" name="gender" id="male" value="male"> Male<br>
<input type="radio" name="gender" id="female" value="female"> Female<br>
<input type="radio" name="gender" id="other" value="other"> Other
<br><br>

<h3 class="required">When is your birthday?</h3>
<!--Enter Birthday-->
<input type="date" name="bday" id="bday" class="bday" for="bday">
<br><br>

<h3 class="required">Do you own any of the following items?</h3>
<h4>(check none if you do not)</h4>
<!--Checkbox-->
<input type="checkbox" name="vehicule1" id="vehicule1" value="Bicycle"> Bicycle<br>
<input type="checkbox" name="vehicule2" id="vehicule2" value="Car"> Car <br>
<input type="checkbox" name="vehicule3" id="vehicule3" value="Motorcycle"> Motorcycle <br>
<input type="checkbox" name="vehicule4" id="vehicule4" value="Tractor"> Tractor <br>
<input type="checkbox" name="vehicule5" id="vehicule5" value="None"> None
<br>


<div class="bottom-form">
<p>_____________________</p>
<br>
<h2>Now, what can we do for you?</h2>

<h3>Enter your website's url to get promoted on ours!</h3>
<!--Enter website url-->
<input type="url" name="websiteUrl" id="websiteUrl" class="url-form"> <br><br>


<h3>All done? Click submit!</h3>
<input class="button" type="button" onclick="function()" value="Submit" >
</div>


</form>
</div>
</div>

<!--Adding the footer-->
<h2 id="footer">Thank you for your trust & fidelity</h2>

</body>








<script>
//Creating the validation function
$(document).ready(function() {
$('form[id="checkout"]').validate({
//Setting up the requirements for form
rules: {
//Check if name is entered
firstname: 'required',
lastname: 'required',

//Check if genders is selected
genders: 'required',

//Check if birthday is entered
bday: 'required',

messages: {
firstname: 'Please enter your first name',
lastname: 'Please enter your last name',
genders: 'Please select your gender',
bday: 'Please enter your birthday',
checkbox: 'Please check at least one box'
},

//Check if at least one checkbox has been filled

//If everything meets the requirements, submit the form
submitHandler: function(form) {
form.submit();
}
};
});
});


function checkboxFunction() {
var checkBox = document.getElementById("checkbox");
var text = document.getElementById("text");
if (checkBox.checked == true){
text.style.display = "none";
} else {
text.style.display = "checkbox";
}
}

</script>

body {
font-family: 'Roboto', sans-serif;
background-color: #E4DFDA;
}

h1 {
letter-spacing: 3px;
font-size: 30pt;
background-color: #4062BB;
height: 110px;
padding-top: 45px;
color: white;
top: 0;
text-align: center;
}

h2 {
font-weight: 200;
}

h3 {
font-weight: 100;
}

.container {
width:100%;
text-align: center;
margin: auto;
}

.form-outline {
width: 35%;
margin: auto;
text-align: left;
}

.bday {
padding: 10px 25px;
border-radius: 5px;
}

.url-form {
padding: 10px 35px;
text-align: left;
}

form {
text-align: left;
}

.bottom-form {
text-align: center;
margin:auto;
}

.required:after {
content:" *";
color: red;
}

.button {
padding: 10px 25px;
color: white;
background-color: #4062BB;
font-size: 14pt;
font-weight: 100;
border-radius: 5px;
border: 3px solid #4062BB;
}

.button:hover {
color: #4062BB;
background-color: transparent;
}

.required:after { content:" *"; }


.error {
color: red;
margin-left: 5px;
}

label.error {
display: inline;
}



#footer {
margin-top: 100px;
background-color: grey;
padding-top: 45px;
padding-bottom: 45px;
color: white;
bottom: 0;
text-align: center;

}

<body>

<h1>Checkout</h1>
<br>
<div class="container">
<h2>Almost done! All we need now is a little bit more about you.</h2>
<h3><em>Please fill all the fields marked with <strong style="color:red;">*</strong> before submitting.</em></h3>
<br>
<p>_____________________</p>
<br>
<div class="form-outline">
<!--Creating the form-->
<form action="https://formspree.io/prometheus111222333@gmail.com" method="POST" name="CheckoutForm" onsubmit="FormValidate()" id="checkout">
<h3 class="required">What is your name?</h3>
<!--Enter name-->
<input type="text" name="firstname" id="firstname" placeholder="First name" style="padding:10px; border-radius:3px;" for="firstname">

<input type="text" name="lastname" id="lastname" placeholder="Last name" style="padding:10px; border-radius:3px;" for="lastname">
<br><br>
<!--Display client ID (can't be modified)-->
<em>Client ID</em><br><br>
<input type="text" name="id" value="#1322247" style="text-align:center; padding:10px; border-radius:3px;" disabled>
<br><br>

<!--Select gender-->
<h3 class="required" id="genders" for="genders">What is your gender?</h3>
<input type="radio" name="gender" id="male" value="male"> Male<br>
<input type="radio" name="gender" id="female" value="female"> Female<br>
<input type="radio" name="gender" id="other" value="other"> Other
<br><br>

<h3 class="required">When is your birthday?</h3>
<!--Enter Birthday-->
<input type="date" name="bday" id="bday" class="bday" for="bday">
<br><br>

<h3 class="required">Do you own any of the following items?</h3>
<h4>(check none if you do not)</h4>
<!--Checkbox-->
<input type="checkbox" name="vehicule1" id="vehicule1" value="Bicycle"> Bicycle<br>
<input type="checkbox" name="vehicule2" id="vehicule2" value="Car"> Car <br>
<input type="checkbox" name="vehicule3" id="vehicule3" value="Motorcycle"> Motorcycle <br>
<input type="checkbox" name="vehicule4" id="vehicule4" value="Tractor"> Tractor <br>
<input type="checkbox" name="vehicule5" id="vehicule5" value="None"> None
<br>


<div class="bottom-form">
<p>_____________________</p>
<br>
<h2>Now, what can we do for you?</h2>

<h3>Enter your website's url to get promoted on ours!</h3>
<!--Enter website url-->
<input type="url" name="websiteUrl" id="websiteUrl" class="url-form"> <br><br>


<h3>All done? Click submit!</h3>
<input class="button" type="button" onclick="function()" value="Submit" >
</div>


</form>
</div>
</div>

<!--Adding the footer-->
<h2 id="footer">Thank you for your trust & fidelity</h2>

</body>





<script>
//Creating the validation function
$(document).ready(function() {
$('form[id="checkout"]').validate({
//Setting up the requirements for form
rules: {
//Check if name is entered
firstname: 'required',
lastname: 'required',

//Check if genders is selected
genders: 'required',

//Check if birthday is entered
bday: 'required',

messages: {
firstname: 'Please enter your first name',
lastname: 'Please enter your last name',
genders: 'Please select your gender',
bday: 'Please enter your birthday',
checkbox: 'Please check at least one box'
},

//Check if at least one checkbox has been filled

//If everything meets the requirements, submit the form
submitHandler: function(form) {
form.submit();
}
};
});
});


function checkboxFunction() {
var checkBox = document.getElementById("checkbox");
var text = document.getElementById("text");
if (checkBox.checked == true){
text.style.display = "none";
} else {
text.style.display = "checkbox";
}
}

</script>

body {
font-family: 'Roboto', sans-serif;
background-color: #E4DFDA;
}

h1 {
letter-spacing: 3px;
font-size: 30pt;
background-color: #4062BB;
height: 110px;
padding-top: 45px;
color: white;
top: 0;
text-align: center;
}

h2 {
font-weight: 200;
}

h3 {
font-weight: 100;
}

.container {
width:100%;
text-align: center;
margin: auto;
}

.form-outline {
width: 35%;
margin: auto;
text-align: left;
}

.bday {
padding: 10px 25px;
border-radius: 5px;
}

.url-form {
padding: 10px 35px;
text-align: left;
}

form {
text-align: left;
}

.bottom-form {
text-align: center;
margin:auto;
}

.required:after {
content:" *";
color: red;
}

.button {
padding: 10px 25px;
color: white;
background-color: #4062BB;
font-size: 14pt;
font-weight: 100;
border-radius: 5px;
border: 3px solid #4062BB;
}

.button:hover {
color: #4062BB;
background-color: transparent;
}

.required:after { content:" *"; }


.error {
color: red;
margin-left: 5px;
}

label.error {
display: inline;
}



#footer {
margin-top: 100px;
background-color: grey;
padding-top: 45px;
padding-bottom: 45px;
color: white;
bottom: 0;
text-align: center;

}

<body>

<h1>Checkout</h1>
<br>
<div class="container">
<h2>Almost done! All we need now is a little bit more about you.</h2>
<h3><em>Please fill all the fields marked with <strong style="color:red;">*</strong> before submitting.</em></h3>
<br>
<p>_____________________</p>
<br>
<div class="form-outline">
<!--Creating the form-->
<form action="https://formspree.io/prometheus111222333@gmail.com" method="POST" name="CheckoutForm" onsubmit="FormValidate()" id="checkout">
<h3 class="required">What is your name?</h3>
<!--Enter name-->
<input type="text" name="firstname" id="firstname" placeholder="First name" style="padding:10px; border-radius:3px;" for="firstname">

<input type="text" name="lastname" id="lastname" placeholder="Last name" style="padding:10px; border-radius:3px;" for="lastname">
<br><br>
<!--Display client ID (can't be modified)-->
<em>Client ID</em><br><br>
<input type="text" name="id" value="#1322247" style="text-align:center; padding:10px; border-radius:3px;" disabled>
<br><br>

<!--Select gender-->
<h3 class="required" id="genders" for="genders">What is your gender?</h3>
<input type="radio" name="gender" id="male" value="male"> Male<br>
<input type="radio" name="gender" id="female" value="female"> Female<br>
<input type="radio" name="gender" id="other" value="other"> Other
<br><br>

<h3 class="required">When is your birthday?</h3>
<!--Enter Birthday-->
<input type="date" name="bday" id="bday" class="bday" for="bday">
<br><br>

<h3 class="required">Do you own any of the following items?</h3>
<h4>(check none if you do not)</h4>
<!--Checkbox-->
<input type="checkbox" name="vehicule1" id="vehicule1" value="Bicycle"> Bicycle<br>
<input type="checkbox" name="vehicule2" id="vehicule2" value="Car"> Car <br>
<input type="checkbox" name="vehicule3" id="vehicule3" value="Motorcycle"> Motorcycle <br>
<input type="checkbox" name="vehicule4" id="vehicule4" value="Tractor"> Tractor <br>
<input type="checkbox" name="vehicule5" id="vehicule5" value="None"> None
<br>


<div class="bottom-form">
<p>_____________________</p>
<br>
<h2>Now, what can we do for you?</h2>

<h3>Enter your website's url to get promoted on ours!</h3>
<!--Enter website url-->
<input type="url" name="websiteUrl" id="websiteUrl" class="url-form"> <br><br>


<h3>All done? Click submit!</h3>
<input class="button" type="button" onclick="function()" value="Submit" >
</div>


</form>
</div>
</div>

<!--Adding the footer-->
<h2 id="footer">Thank you for your trust & fidelity</h2>

</body>






jquery html forms validation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 4:56









Shiladitya

9,36891830




9,36891830










asked Nov 11 at 2:34









Giacomo Esquerré

61




61








  • 1




    You should use something like github.com/jquery-validation/jquery-validation
    – Christopher Dosin
    Nov 11 at 4:41














  • 1




    You should use something like github.com/jquery-validation/jquery-validation
    – Christopher Dosin
    Nov 11 at 4:41








1




1




You should use something like github.com/jquery-validation/jquery-validation
– Christopher Dosin
Nov 11 at 4:41




You should use something like github.com/jquery-validation/jquery-validation
– Christopher Dosin
Nov 11 at 4:41












1 Answer
1






active

oldest

votes

















up vote
0
down vote













You should use something like these:



$("#AddProductCategory").validate({
errorElement: 'span', //default input error message container
errorClass: 'help-block help-block-error', // default input error message class
focusInvalid: false, // do not focus the last invalid input
ignore: "", // validate all fields including form hidden input
excluded: ':disabled',
rules: {
Title: {
minlength: 2,
required: true,
maxlength:100
},
EnglishTitle: {
minlength: 2,
required: true,
maxlength: 100
},
IsProductLine: {
required: true
},
IsVisible: {
required: true
},
Image: {
accept: "png|jpe?g|gif",
filesize: 150000
}
},
messages: {
Image: {
accept: "نوع فایل ورود باید .png .jpeg .png باشد",
filesize:"سایز عکس انتخابی حداکثر می تواند 150KB باشد"
}
},

invalidHandler: function (event, validator) { //display error alert on form submit
ModalAlert("DivAlert", "فرم دارای خطا می باشد", "danger");
$('.modal').animate({ scrollTop: 0 }, 'slow');
},

errorPlacement: function (error, element) { // render error placement for each input type
if (element.parents('.mt-radio-list').size() > 0 || element.parents('.mt-checkbox-list').size() > 0) {
if (element.parents('.mt-radio-list').size() > 0) {
error.appendTo(element.parents('.mt-radio-list')[0]);
}
if (element.parents('.mt-checkbox-list').size() > 0) {
error.appendTo(element.parents('.mt-checkbox-list')[0]);
}
} else if (element.parents('.mt-radio-inline').size() > 0 || element.parents('.mt-checkbox-inline').size() > 0) {
if (element.parents('.mt-radio-inline').size() > 0) {
error.appendTo(element.parents('.mt-radio-inline')[0]);
}
if (element.parents('.mt-checkbox-inline').size() > 0) {
error.appendTo(element.parents('.mt-checkbox-inline')[0]);
}
} else if (element.parent(".input-group").size() > 0) {
error.insertAfter(element.parent(".input-group"));
} else if (element.attr("data-error-container")) {
error.appendTo(element.attr("data-error-container"));
} else {
error.insertAfter(element); // for other inputs, just perform default behavior
}
},

highlight: function (element) { // hightlight error inputs

$(element)
.closest('.form-group').addClass('has-error'); // set error class to the control group
},

unhighlight: function (element) { // revert the change done by hightlight

$(element)
.closest('.form-group').removeClass('has-error'); // set error class to the control group
},

success: function (label) {
label
.closest('.form-group').removeClass('has-error'); // set success class to the control group

},

submitHandler: function (form) {

CreateProductCategory();
$('.modal').animate({ scrollTop: 0 }, 'slow');
}
});





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245365%2fhow-to-validate-a-form-with-jquery%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You should use something like these:



    $("#AddProductCategory").validate({
    errorElement: 'span', //default input error message container
    errorClass: 'help-block help-block-error', // default input error message class
    focusInvalid: false, // do not focus the last invalid input
    ignore: "", // validate all fields including form hidden input
    excluded: ':disabled',
    rules: {
    Title: {
    minlength: 2,
    required: true,
    maxlength:100
    },
    EnglishTitle: {
    minlength: 2,
    required: true,
    maxlength: 100
    },
    IsProductLine: {
    required: true
    },
    IsVisible: {
    required: true
    },
    Image: {
    accept: "png|jpe?g|gif",
    filesize: 150000
    }
    },
    messages: {
    Image: {
    accept: "نوع فایل ورود باید .png .jpeg .png باشد",
    filesize:"سایز عکس انتخابی حداکثر می تواند 150KB باشد"
    }
    },

    invalidHandler: function (event, validator) { //display error alert on form submit
    ModalAlert("DivAlert", "فرم دارای خطا می باشد", "danger");
    $('.modal').animate({ scrollTop: 0 }, 'slow');
    },

    errorPlacement: function (error, element) { // render error placement for each input type
    if (element.parents('.mt-radio-list').size() > 0 || element.parents('.mt-checkbox-list').size() > 0) {
    if (element.parents('.mt-radio-list').size() > 0) {
    error.appendTo(element.parents('.mt-radio-list')[0]);
    }
    if (element.parents('.mt-checkbox-list').size() > 0) {
    error.appendTo(element.parents('.mt-checkbox-list')[0]);
    }
    } else if (element.parents('.mt-radio-inline').size() > 0 || element.parents('.mt-checkbox-inline').size() > 0) {
    if (element.parents('.mt-radio-inline').size() > 0) {
    error.appendTo(element.parents('.mt-radio-inline')[0]);
    }
    if (element.parents('.mt-checkbox-inline').size() > 0) {
    error.appendTo(element.parents('.mt-checkbox-inline')[0]);
    }
    } else if (element.parent(".input-group").size() > 0) {
    error.insertAfter(element.parent(".input-group"));
    } else if (element.attr("data-error-container")) {
    error.appendTo(element.attr("data-error-container"));
    } else {
    error.insertAfter(element); // for other inputs, just perform default behavior
    }
    },

    highlight: function (element) { // hightlight error inputs

    $(element)
    .closest('.form-group').addClass('has-error'); // set error class to the control group
    },

    unhighlight: function (element) { // revert the change done by hightlight

    $(element)
    .closest('.form-group').removeClass('has-error'); // set error class to the control group
    },

    success: function (label) {
    label
    .closest('.form-group').removeClass('has-error'); // set success class to the control group

    },

    submitHandler: function (form) {

    CreateProductCategory();
    $('.modal').animate({ scrollTop: 0 }, 'slow');
    }
    });





    share|improve this answer

























      up vote
      0
      down vote













      You should use something like these:



      $("#AddProductCategory").validate({
      errorElement: 'span', //default input error message container
      errorClass: 'help-block help-block-error', // default input error message class
      focusInvalid: false, // do not focus the last invalid input
      ignore: "", // validate all fields including form hidden input
      excluded: ':disabled',
      rules: {
      Title: {
      minlength: 2,
      required: true,
      maxlength:100
      },
      EnglishTitle: {
      minlength: 2,
      required: true,
      maxlength: 100
      },
      IsProductLine: {
      required: true
      },
      IsVisible: {
      required: true
      },
      Image: {
      accept: "png|jpe?g|gif",
      filesize: 150000
      }
      },
      messages: {
      Image: {
      accept: "نوع فایل ورود باید .png .jpeg .png باشد",
      filesize:"سایز عکس انتخابی حداکثر می تواند 150KB باشد"
      }
      },

      invalidHandler: function (event, validator) { //display error alert on form submit
      ModalAlert("DivAlert", "فرم دارای خطا می باشد", "danger");
      $('.modal').animate({ scrollTop: 0 }, 'slow');
      },

      errorPlacement: function (error, element) { // render error placement for each input type
      if (element.parents('.mt-radio-list').size() > 0 || element.parents('.mt-checkbox-list').size() > 0) {
      if (element.parents('.mt-radio-list').size() > 0) {
      error.appendTo(element.parents('.mt-radio-list')[0]);
      }
      if (element.parents('.mt-checkbox-list').size() > 0) {
      error.appendTo(element.parents('.mt-checkbox-list')[0]);
      }
      } else if (element.parents('.mt-radio-inline').size() > 0 || element.parents('.mt-checkbox-inline').size() > 0) {
      if (element.parents('.mt-radio-inline').size() > 0) {
      error.appendTo(element.parents('.mt-radio-inline')[0]);
      }
      if (element.parents('.mt-checkbox-inline').size() > 0) {
      error.appendTo(element.parents('.mt-checkbox-inline')[0]);
      }
      } else if (element.parent(".input-group").size() > 0) {
      error.insertAfter(element.parent(".input-group"));
      } else if (element.attr("data-error-container")) {
      error.appendTo(element.attr("data-error-container"));
      } else {
      error.insertAfter(element); // for other inputs, just perform default behavior
      }
      },

      highlight: function (element) { // hightlight error inputs

      $(element)
      .closest('.form-group').addClass('has-error'); // set error class to the control group
      },

      unhighlight: function (element) { // revert the change done by hightlight

      $(element)
      .closest('.form-group').removeClass('has-error'); // set error class to the control group
      },

      success: function (label) {
      label
      .closest('.form-group').removeClass('has-error'); // set success class to the control group

      },

      submitHandler: function (form) {

      CreateProductCategory();
      $('.modal').animate({ scrollTop: 0 }, 'slow');
      }
      });





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You should use something like these:



        $("#AddProductCategory").validate({
        errorElement: 'span', //default input error message container
        errorClass: 'help-block help-block-error', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        ignore: "", // validate all fields including form hidden input
        excluded: ':disabled',
        rules: {
        Title: {
        minlength: 2,
        required: true,
        maxlength:100
        },
        EnglishTitle: {
        minlength: 2,
        required: true,
        maxlength: 100
        },
        IsProductLine: {
        required: true
        },
        IsVisible: {
        required: true
        },
        Image: {
        accept: "png|jpe?g|gif",
        filesize: 150000
        }
        },
        messages: {
        Image: {
        accept: "نوع فایل ورود باید .png .jpeg .png باشد",
        filesize:"سایز عکس انتخابی حداکثر می تواند 150KB باشد"
        }
        },

        invalidHandler: function (event, validator) { //display error alert on form submit
        ModalAlert("DivAlert", "فرم دارای خطا می باشد", "danger");
        $('.modal').animate({ scrollTop: 0 }, 'slow');
        },

        errorPlacement: function (error, element) { // render error placement for each input type
        if (element.parents('.mt-radio-list').size() > 0 || element.parents('.mt-checkbox-list').size() > 0) {
        if (element.parents('.mt-radio-list').size() > 0) {
        error.appendTo(element.parents('.mt-radio-list')[0]);
        }
        if (element.parents('.mt-checkbox-list').size() > 0) {
        error.appendTo(element.parents('.mt-checkbox-list')[0]);
        }
        } else if (element.parents('.mt-radio-inline').size() > 0 || element.parents('.mt-checkbox-inline').size() > 0) {
        if (element.parents('.mt-radio-inline').size() > 0) {
        error.appendTo(element.parents('.mt-radio-inline')[0]);
        }
        if (element.parents('.mt-checkbox-inline').size() > 0) {
        error.appendTo(element.parents('.mt-checkbox-inline')[0]);
        }
        } else if (element.parent(".input-group").size() > 0) {
        error.insertAfter(element.parent(".input-group"));
        } else if (element.attr("data-error-container")) {
        error.appendTo(element.attr("data-error-container"));
        } else {
        error.insertAfter(element); // for other inputs, just perform default behavior
        }
        },

        highlight: function (element) { // hightlight error inputs

        $(element)
        .closest('.form-group').addClass('has-error'); // set error class to the control group
        },

        unhighlight: function (element) { // revert the change done by hightlight

        $(element)
        .closest('.form-group').removeClass('has-error'); // set error class to the control group
        },

        success: function (label) {
        label
        .closest('.form-group').removeClass('has-error'); // set success class to the control group

        },

        submitHandler: function (form) {

        CreateProductCategory();
        $('.modal').animate({ scrollTop: 0 }, 'slow');
        }
        });





        share|improve this answer












        You should use something like these:



        $("#AddProductCategory").validate({
        errorElement: 'span', //default input error message container
        errorClass: 'help-block help-block-error', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        ignore: "", // validate all fields including form hidden input
        excluded: ':disabled',
        rules: {
        Title: {
        minlength: 2,
        required: true,
        maxlength:100
        },
        EnglishTitle: {
        minlength: 2,
        required: true,
        maxlength: 100
        },
        IsProductLine: {
        required: true
        },
        IsVisible: {
        required: true
        },
        Image: {
        accept: "png|jpe?g|gif",
        filesize: 150000
        }
        },
        messages: {
        Image: {
        accept: "نوع فایل ورود باید .png .jpeg .png باشد",
        filesize:"سایز عکس انتخابی حداکثر می تواند 150KB باشد"
        }
        },

        invalidHandler: function (event, validator) { //display error alert on form submit
        ModalAlert("DivAlert", "فرم دارای خطا می باشد", "danger");
        $('.modal').animate({ scrollTop: 0 }, 'slow');
        },

        errorPlacement: function (error, element) { // render error placement for each input type
        if (element.parents('.mt-radio-list').size() > 0 || element.parents('.mt-checkbox-list').size() > 0) {
        if (element.parents('.mt-radio-list').size() > 0) {
        error.appendTo(element.parents('.mt-radio-list')[0]);
        }
        if (element.parents('.mt-checkbox-list').size() > 0) {
        error.appendTo(element.parents('.mt-checkbox-list')[0]);
        }
        } else if (element.parents('.mt-radio-inline').size() > 0 || element.parents('.mt-checkbox-inline').size() > 0) {
        if (element.parents('.mt-radio-inline').size() > 0) {
        error.appendTo(element.parents('.mt-radio-inline')[0]);
        }
        if (element.parents('.mt-checkbox-inline').size() > 0) {
        error.appendTo(element.parents('.mt-checkbox-inline')[0]);
        }
        } else if (element.parent(".input-group").size() > 0) {
        error.insertAfter(element.parent(".input-group"));
        } else if (element.attr("data-error-container")) {
        error.appendTo(element.attr("data-error-container"));
        } else {
        error.insertAfter(element); // for other inputs, just perform default behavior
        }
        },

        highlight: function (element) { // hightlight error inputs

        $(element)
        .closest('.form-group').addClass('has-error'); // set error class to the control group
        },

        unhighlight: function (element) { // revert the change done by hightlight

        $(element)
        .closest('.form-group').removeClass('has-error'); // set error class to the control group
        },

        success: function (label) {
        label
        .closest('.form-group').removeClass('has-error'); // set success class to the control group

        },

        submitHandler: function (form) {

        CreateProductCategory();
        $('.modal').animate({ scrollTop: 0 }, 'slow');
        }
        });






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 5:37









        shaghayegh sheykholeslami

        1847




        1847






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245365%2fhow-to-validate-a-form-with-jquery%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma