As an exercise I have added some parsley validation to this form here
http://matthewpiatetsky.com/cs401/login.html
I am now trying to add a similar validation to the form here
http://matthewpiatetsky.com/cs401/writereviews.html
(I have some mouseout validation on the text area)
I am running into two issues:
How can I get the form to validate before the form submits to its action? Can I run validation first, and if validation succeeds, then submit the form.
How can I add data-required="true" to the jquery raty stars to force the user to fill those in?
Thanks!
For validation before submit you can do something like:
var form = document.getElementById(“yourFormID”);
form.addEventListener("submit", function(event) {
// do your additional validation here
if (something wrong)
event.preventDefault();
}, false);
Related
I am validating a form using Parlsey like so:
$(document).on("click", ".submit-form-btn", function(e) {
$(".form-class").off().submit(function(e) {
e.preventDefault();
$(this).parsley().validate();
if ($(this).parsley().isValid()) {
//Do something
}
});
});
After the initial validation, validation checks appear to be calling on every "keydown" press. How can I only validate again when my ".submit-form-btn" is clicked on again? I only want validation checks on click.
You can achieve this by adding the data-parsley-trigger-after-failure attribute with a value of submit. This will then ensure only submits will trigger further validation. You can either add it to individual fields (causing some fields to continue to validate on input while others will only validate on submit), or to the entire form (to specify submit-only validation everywhere):
<form id="form" data-parsley-trigger-after-failure="submit">
or:
<input type="text" required data-parsley-trigger-after-failure="submit">
Here's a Codepen example.
I have a form which is validated using the jQuery validation plugin. I added the bootstrap confirmation on the submit button. Everything is working fine, except that I want the bootstrap confirmation to appear on the submit button when the form no longer has any errors.
I've tried doing this:
HTML code:
<button type="submit" class="btn" id="submit_button" data-toggle="confirmation"data-popout="true">Submit</button>
jQuery code:
$("#submit_button").on('click', function(){
if($('#form').valid()) {
$(this).trigger("confirmation");
}
});
But it didn't do anything. Should I use the submitHandler in jQuery validation? Or is this not just possible?
Should I use the submitHandler in jQuery validation?
YES, of course.
Use the submitHandler for this. As per documentation, it fires only when the form is valid, so there's no need to test the form's validity or use another click handler.
$('#form').validate({
// options, etc.
submitHandler: function(form) { // fires when valid (and on button click)
$("#selector").trigger("confirmation");
}
});
I am using jQuery Validator to validate a form and I wanted to disable the fields that were blank when the user clicks submit so that the server does not receive any data from the inputs that were left blank. To disable them, I used the following Javascript:
$(function() {
$("#form1").submit(function() {
$(this).find(":input").filter(function(){ return !this.value; }).attr("disabled", "disabled");
return true; // ensure form still submits
});
});
Here is my problem: This code works great assuming the form was validated on the first submission. However, if the user hits submit but it was not validated, all of the alerts for various required fields and such pop-up, but all of the inputs are disabled. How can I fix this? I need to know how to re-enable them if the form is not validated and properly submitted.
It doesn't make sense to disable the field to submit. Rather—you should first do a front-end only validation that prevents submitting if there are empty fields (and those fields are required to not be empty).
You can set up a variable (or a function) to run through your validation and only submit if it returns true. This is a condensed example of just one input.
var validate = ($('input').val() == '') ? false : true;
if(validate){
// submit your form
} else {
// throw your errors
}
Then it would be good to clear your errors at the beginning of your validation.
I am trying to dynamically disable the parsley.js max length on several of my input fields when the user makes a selection on a select list on the form.
I have read this thread, but when I put the code into my field, the parsley is not triggered, instead the form submits, and I do not understand why.
I have read the parsley.js docs, but I am unable to see why the parsley.js validation is ignored when i add the following line of code:
$('#id_employment_record_position_title').attr('data-parsley-maxlength', '0');
or
$('#id_employment_record_position_title').attr('data-parsley-maxlength', '150');
This is my code to dynamically turm the parsley validation on and off when the user changes the select list on the form:
function toggleFormDetails() {
if ( $('#id_employment_record_display_type').val() == '8888' || $('#id_employment_record_display_type').val() == '9999' ) {
//disable the input field.
$('#id_employment_record_position_title').prop('disabled', true);
....
//destroy parsley on the form.
//$('#employment_history_details_form').parsley().destroy();
//disable the parsley maxlength, when the input field is disabled.
$('#id_employment_record_position_title').attr('data-parsley-maxlength', '0');
//reinitialise parsley on the form.
//$('#employment_history_details_form').parsley();
} else {
//enable the input field.
$('#id_employment_record_position_title').prop('disabled', false);
....
//destroy parsley on the form.
//$('#employment_history_details_form').parsley().destroy();
//change the parsley cs error values for all the required form inputs.
$('#id_employment_record_position_title').attr('data-parsley-maxlength', '150');
//reinitialise parsley on the form.
//$('#employment_history_details_form').parsley();
}
}
Why do I have have to add the destroy & create parsley code on the form (I have commented them out above)?
Would it be better to write a custom validation for this? If so, how would I do that, b/c my js code skills are not yet good enough?
Parsley is a Javascript library and works something like this:
When you render your html form, you specify the needed validations through data attributes.
You indicate that the form will be validated by Parsley either with the form attribute data-parsley-validate or via javascript through $("#form").parsley().
When parsley is binded to your form (the second step), an object of the type ParsleyForm will be created containing the constraints for each field.
After the object is created, the html attributes are no longer needed. So, if you change any attribute it will have no impact on the validation since Parsley will verify the constraints of the javascript object. This is why you need to destroy & bind parsley (in order to recreate the parsley instance with the new constraints).
In order to solve your issue, you could do something like this:
<form id="myForm" class="form-horizontal" method="post">
<input type="text" name="id_employment_record_display_type"
id="id_employment_record_display_type"
placeholder="Set 8888 to discard maxlength validation" />
<input type="text" name="sample" id="id_employment_record_position_title"
data-parsley-maxlength="150" />
<button type="submit">Submit</button>
</form>
<script>
$(document).ready(function() {
$("#myForm").parsley();
$("#id_employment_record_display_type").on('change', function() {
$("#myForm").parsley().destroy();
if ( $(this).val() == '8888' || $(this).val() == '9999' ) {
$('#id_employment_record_position_title').removeAttr('data-parsley-maxlength');
$('#id_employment_record_position_title').prop('disabled', true);
} else {
$('#id_employment_record_position_title').attr('data-parsley-maxlength', '150');
$('#id_employment_record_position_title').prop('disabled', false);
}
$("#myForm").parsley();
});
$("#myForm").submit(function() {
$(this).parsley().validate();
// when there are no client side errors when the form is submitted
if ($(this).parsley().isValid()) {
console.log('no client side errors!');
} else {
console.log('form is not valid');
}
event.preventDefault();
});
});
</script>
You can also check this working jsfiddle
I have a form, on which I have a button - When the user clicks the button Add, a function is called which adds a new row to a table in the same form, but using javascript.
I have used the jquery validation plugin,the problem I have is that the validation only fires when I use a submit button,but not when my button is clicked, which runs my function.
I understand why this is - The validation plugin wants the form to be submitted.
Is there any way I can cause the validation to run and return its result? My function can then either do nothing if the validation fails, or go ahead and post the user entered data to my function if the validation passes. Obviously I would need to do this without the form actually submitting.
Any help greatly appreciated - I've tried a few ideas I found whilst Googling on this, but nothing has worked so far.
Thank you beforehand.
If you mean http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/
if ($j('#Form1').validationEngine({ returnIsValid: true })) {
//is valid
}
Or if you mean: http://docs.jquery.com/Plugins/Validation
$("#myform").validate({
submitHandler: function(form) {
// some other code
return false;
}
});