Welcome, Guest
  • Page:
  • 1

TOPIC: validate txt if checkbox checked

validate txt if checkbox checked 1 year, 10 months ago #14660

Hi,

I'm trying to add custom validation to a form built in Classic Mode. If a checkbox is checked then I want to validate that the corresponding text field has been entered. Please see attached diagram to explain further. I have tried to piece together bits of script but can't get anything to work and don't really know what I'm doing. Any help would be great!

thanks
forms_query.png
  • lrawling
  • OFFLINE
  • Junior Boarder
  • Posts: 21
  • Karma: 0

Re: validate txt if checkbox checked 1 year, 10 months ago #14682

Wow, I have a question and someone just asked it. In my case, I have a text box that is (or will be) hidden unless the checkbox is checked. If checked, I want the textbox to be required, but not required if the checkbox is not checked just as the original post requests. I don't know if the hiding action will change the necessity. I am using the Easy Eorm Method.

Thanx,
Steven
  • neo314
  • OFFLINE
  • Fresh Boarder
  • Posts: 8
  • Karma: 0

Re: validate txt if checkbox checked 1 year, 10 months ago #14693

I would like an answer on best practices, but in the meantime, I think I can solve you issue. It is working on my form anyway. I am using jQuery which I load in compatibility mode, so if you don't you'll have to work out the mootools difference or load jQuery. Also, I am using EasyMode which I understand to not be the common choice.

I could not figure out how to get the syntax correct for custom validation code within the element, so I created a function in the available scripts and made a package of scripts just for this web site (vrm which you will see as a function prefix).

I created a new validation script under "Manage Scripts" and gave it the package VRM to separate it from the built in functions. It's type is "Element Validation." The code is:

function vrm_applianceInfoValidate(element,message) {
if (jQuery("#ff_elem206").is(":checked")) {
  if (element.value == '') {
    if (message=='') {
      message = "If this is an appliance problem, we need the make and model of the appliance.";
    }
    ff_validationFocus(element.name);
    return message;
  } else {
    return '';
  }
} else {
  return '';
}
}


(Updated: I added the final return '' code to the if checked statement. Without it, validation returns an "undefined" if the checkbox is not checked.)


I could perhaps use jQuery to search for the checkbox before this element to make it more reusable, but that is still pretty restrictive, so I just target this script to the controlling element, "#ff_elem206" which is a checkbox.

In my case, I also created a toggle script for the checkbox to show/hide the textbox item, and a script to initially hide any element. What I found was that when the validation returns an error, the form is reinitialized, so my textbox would be hidden while the checkbox was still checked (opposite of intended results). To resolve that, I had to create a specific hiding script for the element that checked the value of the checkbox on form load to decide whether or not to hide the element.
  • neo314
  • OFFLINE
  • Fresh Boarder
  • Posts: 8
  • Karma: 0
Last Edit: 1 year, 10 months ago by neo314.
  • Page:
  • 1
Moderators: TheMuffinMan, ForumSupport
Time to create page: 0.34 seconds

Facebook Discount - 40% Off!

Fans of our Facebook page here are entitled to get a 1-year-subscription for the price of a 6-months!

Steps to take:

  • Become a fan on Facebook (Like) here
  • Purchase a 6-months-subscription from here
  • Write a quick email to This email address is being protected from spambots. You need JavaScript enabled to view it. with your username and prove of your like and we'll upgrade to the 1-year-subscription

Joomla!® Forms Discount - 40% Off!

Already purchased a forms extension from a different vendor but your requirements changed and now you need a forms extension that is capable of more than just the standards? We help you to keep the costs for a change under control: Get a 40% discount on our professional membership plan! -- meaning timely unlimited access to all current and future commercial extensions, breezingforms themes & apps and Joomla!® templates at crosstec.de, including 1 year of support -- without any website limitations -- Just prove that you previously purchased another forms extension for Joomla!® from a different vendor and you are qualified.

Steps to take:

  • Purchase a 1-year-subscription from here
  • Write a quick email to This email address is being protected from spambots. You need JavaScript enabled to view it. with your username and prove of the purchase for the other form vendor and we'll upgrade to the professional-subscription

About

Crosstec GmbH & Co. KG

Bergisch-Gladbacher-Str. 829

51069 Cologne, Germany

Imprint