Forums

How to validate form fields in a not submit button with gvalidation

hgaleano 10 Sep, 2015
Hi everybody!

I’m trying to do something in my web site, but first I need to know if I can do this. I explain my scenario
1. I have a form with a Tab area and other fields. The tab area has 2 tabs
2. In the second tab there are some fields and a button to save ONLY these fields. This is not a submit button, it will save the record (just the fields in tab 2) through AJAX.
3. Some of the fields, in the tab, are defined as required or they have other validation rules (alpha, digit, phone etc). I configured these validations in the validations tab of each field (in CF designer) . When I run the form, and I test the fields, everything is fine with the validations, the popup messages are shown.
4. The button has an on-click event with some other validations in JS.

Here is my question
I know that when a form is submitted, it is validated by the validation routine and the submission is stopped if some field is invalid. But in this case, my button is not a submit button, How can I know which fields are invalid, when click the button? How can I call, in my click event, the CF validation routine for each field in order to receive a true or false value, according to the rules defined?

Thanks in advance
Hugo
GreyHead 10 Sep, 2015
Hi Hugo,

I have no idea how to do that. You can probably fire the validation - but it will then validate all the fields, not just the ones in your sub-form.

Bob
hgaleano 10 Sep, 2015
Answer
Thanks Bob!! for your quick answer.
After your answer I made an analysis of gvalidation.js, I found my own solution. I leave it here, in case anyone needs some similar. It works for me, I hope it helps somebody.
function validAll()
{
	vCntErr = 0;
/*formHolder is a container for my sub-form*/
	jQuery( '#formHolder :input' ).each(function( index, element ) {
		jQuery(element).change();	/*force field validation*/
                /*if the field does not accomplish some rule, the rule name is stored in invalid_rule property. i.e. "required" */
		vInvalid_rule = jQuery(element).data().gvalidation.invalid_rule;
		if (vInvalid_rule != "")
			vCntErr += 1;
	});
/*the function returns the number of errors in sub-form*/
	return vCntErr;
}
This topic is locked and no more replies can be posted.