function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ClintLeeClintLee 

JQuery Form Validation - Checkbox Issues

Hi,

 

I have a VF page with a form included on it that I am validating with jquery.  Thanks to some previous posts by @weznolte and @tehnrd I was able to create some pretty slick validation rules, but I am stumped on an issue with a checkbox that seems like it should be relatively straightforward.

 

What I need to accomplish is simply this - if the checkbox is checked then make a field required.  If the checkbox is not checked, then this field is not required. 

 

The variable "isdedicated" holds the ID of the checkbox.

The variable "recipFName" holds the ID of the field that needs to be conditionally required based on the checkbox.

The last rule in the first set of code is my (ump-teenth) attempt at making this work. 

 

It seems that the value of my rule function always returns true, because when the form validates "recipFName" is always required regardless of whether the checkbox is checked or not.

 

 

Here is the code to invoke the validate() method and includes the rules.

 

<script type="text/javascript">
var j$ = jQuery.noConflict();

j$(document).ready(function() {
j$(jq(f)).validate();

j$(jq(dFName)).rules("add",{
required: true,
minlength: 2
});

j$(jq(dLName)).rules("add",{
required: true,
minlength: 2
});

j$(jq(dEmail)).rules("add",{
required: true,
email: true
});

j$(jq(dAddress)).rules("add",{
required: true,
minlength: 5
});

j$(jq(dCity)).rules("add",{
required: true,
minlength: 5
});

j$(jq(dState)).rules("add",{
required: true,
minlength: 2
});

j$(jq(dZip)).rules("add",{
required: true,
minlength: 5
});

j$(jq(dCountry)).rules("add",{
required: true,
minlength: 2
});

j$(jq(dAmount)).rules("add",{
required: true
});

j$(jq(rFName)).rules("add",{
required: function jq() { return
j$(jq(isDedicated)).is(':checked') == true;
}
});

jQuery.validator.messages.required = "Oops! This field is required!";
});
</script>

 

 

And this is the form code.

 

<apex:form id="donForm">
<script type="text/javascript">
var dFName = "{!$Component.donorFirstName}";
var dLName = "{!$Component.donorLastName}";
var dEmail = "{!$Component.donorEmailAddress}";
var dAddress = "{!$Component.donorAddressStreet}";
var dCity = "{!$Component.donorAddressCity}";
var dState = "{!$Component.donorAddressState}";
var dZip = "{!$Component.donorAddressZip}";
var dCountry = "{!$Component.donorAddressCountry}";
var isDedicated = "{!$Component.isDedicated}";
var rFName = "{!$Component.recipFName}";
var dAmount = "{!$Component.donAmount}";
</script>

<apex:outputLabel for="donorFirstName">First Name</apex:outputLabel>
<apex:inputText id="donorFirstName" value="{!donorFName}" />

<apex:outputLabel for="donorLastName">Last Name</apex:outputLabel>
<apex:inputText id="donorLastName" value="{!donorLName}" />

<apex:outputLabel for="acctName">Company Name (optional)</apex:outputLabel>
<apex:inputText id="biz" value="{!acctName}" />

<apex:outputLabel for="donorEmailAddress">Email</apex:outputLabel>
<apex:inputText id="donorEmailAddress" value="{!donorEmail}" />

<h3>Address Information</h3>

<apex:outputLabel for="donorAddressStreet">Street Address</apex:outputLabel>
<apex:inputText id="donorAddressStreet" value="{!donorAddress}" />

<apex:outputLabel for="donorAddressCity">City</apex:outputLabel>
<apex:inputText id="donorAddressCity" value="{!donorCity}" />

<apex:outputLabel for="donorAddressState">State</apex:outputLabel>
<apex:inputText id="donorAddressState" value="{!donorState}" />

<apex:outputLabel for="donorAddressZip">Zip</apex:outputLabel>
<apex:inputText id="donorAddressZip" value="{!donorZip}" />

<apex:outputLabel for="donorAddressCountry">Country</apex:outputLabel>
<apex:inputText id="donorAddressCountry" value="{!donorCountry}" />

<h3>Dedication Info</h3>

<apex:outputLabel for="isdedicated">Dedicate in the name of a friend </apex:outputLabel>
<apex:inputCheckbox id="isDedicated" value="{!isDedicated}"/>


<apex:outputLabel for="recipFName">First Name</apex:outputLabel>
<apex:inputCheckbox id="recipFName" value="{!recipFName}" />

<--Rest of form code here-->
</apex:form>

<script type="text/javascript">
function jq(myid) {
return '#' + myid.replace(/(:|\.)/g,'\\\\$1');
}
var f = '{!$Component.donForm}';
</script>

 

Any help would be much appreciated!  Thanks.

 

Clint

 

incuGuSincuGuS

Hi Billy,

 

Look at this example of the validation plugin :

 

http://jquery.bassistance.de/validate/demo/

 

View the source code and follow its way of initializing the .validation plugin, how it adds the rules on the initialization call.

 

Let me know if that helped you out.

Gaston.

ClintLeeClintLee

Hey Gaston,

 

Thanks for the link.  I reworked the code to match the initialization call on this example, but now when I submit the form it seems that the validate() method isn't being called at all, since the form is being submitted without any validation.  I've been through it several times and have even tested writing the variables the following ways:

 

1. j$(jq(dFName))

2. jq(dFName)

3. dFName

 

Not sure where I'm going wrong, but this has me stumped. 

 

Here is the way I've written it now.

 

<script type="text/javascript">
      var j$ = jquery.noConflict();
      
      j$(document).ready(function() {
         j$(jq(f)).validate({
            rules: {
               j$(jq(dFName)): {
                  required: true,
                  minlength: 2
               },
               j$(jq(dLName)): {
                  required: true,
                  minlength: 2
               },
               j$(jq(dEmail)): {
                  required: true,
                  email: true
               },
               j$(jq(dAddress)): {
                  required: true,
                  minlength: 5
               },
               j$(jq(dCity)): {
                  required: true,
                  minlength: 2
               },
               j$(jq(dState)): {
                  required: true,
                  minlength: 2
               },
               j$(jq(dZip)): {
                  required: true,
                  minlength: 5
               },
               j$(jq(dCountry)): {
                  required: true,
                  minlength: 2
               },
               j$(jq(dAmount)): "required",
               j$(jq(recipFName)): {
                  required: "j$(jq(isdedicated)):checked",
                  minlength: 2
               }       
            },
            messages: {
               j$(jq(dFName)): {
                  required: "Please enter your first name",
                  minlength: "Your first name must be at least 2 letters"
               },
               j$(jq(dLName)): {
                  required: "Please enter your last name",
                  minlength: "Your last name must be at least 2 letters"
               },
               j$(jq(dEmail)): "Please enter your email address",
               j$(jq(dAddress)): "Please enter your street address",
               j$(jq(dCity)): "Please enter your city",
               j$(jq(dState)): "Please enter your state",
               j$(jq(dZip)): "Please enter your zip code",
               j$(jq(dCountry)): "Please enter your country",
               j$(jq(dAmount)): "Please enter your donation amount",
               j$(jq(recipFName)): "Please enter the first name of your dedication recipient"
            }
         });
      });
          
   </script>

 Thanks!

 

Clint

 

incuGuSincuGuS

Billy,

 

Be sure to define the variables containing the ids before calling the validate function so they are available and with values.

Also try a simpler approach first, a form with only 1 input and having it required.

The Ids generated by Salesforce for its components can get tricky, be sure that the validation rule implementation is working for a simple form.

 

If i have some time today ill try creating a page with this and paste you some working code to extend, but ill make no promises.

 

something simpler along the lines of this might help to get it working:

 

 

<apex:form id="donForm">
   <apex:outputLabel for="donorFirstName">First Name</apex:outputLabel>
   <apex:inputText id="donorFirstName" value="{!donorFName}" />
</apex:form>

 

 

Also i noticed you never define the "f" variable that holds the form, maybe you didnt paste it, but check that you are when defining the others :

 

 

   var dFName = "{!$Component.donorFirstName}";
   var dLName = "{!$Component.donorLastName}";
   var dEmail = "{!$Component.donorEmailAddress}";
   var dAddress = "{!$Component.donorAddressStreet}";
   var dCity = "{!$Component.donorAddressCity}";
   var dState = "{!$Component.donorAddressState}";
   var dZip = "{!$Component.donorAddressZip}";
   var dCountry = "{!$Component.donorAddressCountry}";
   var isDedicated = "{!$Component.isDedicated}";
   var rFName = "{!$Component.recipFName}";
   var dAmount = "{!$Component.donAmount}";

 

 

incuGuSincuGuS

Also try using just the ids when defininf the rules like in the example :

 

 

rules: {
  firstname: "required",
  lastname: "required",
  ...

 

 

Instead of what you have :

 

 rules: {
j$(jq(dFName)): {
required: true,
minlength: 2
},

 

just using "dFName" might work. Havent used this plugin before so im just trying to figure out whats wrong :)

Gaston.

 

ClintLeeClintLee

Thanks Gaston. 

 

However, when I use that syntax my form won't validate.  I am using the syntax as seen in this article by Wes Nolte - http://th3silverlining.com/2010/03/02/visualforce-form-validation-enhanced/ - and the form validates as it should, but with one exception.

 

The exception is that when I make certain fields required based on the checkbox being checked, the error messages for those fields don't appear.  All of the other error messages appear as they should, but just the fields that are conditionally required by the checkbox don't display the error messages.  It seems that the validation rules are being set since the form won't submit, but I have no clue as to why the error messages don't display.  Something is amiss.

 

I do appreciate all of your help, though.

 

~Clint

 

 

d3developerd3developer

Are you testing in Firebug? Do you get any error messages?

 

Also, do you need the form element to change whether or not it is required after the page has been loaded?

 

If so, you will need to set a change handler on the checkbox that adds and removes the rules for the other element then re-validates the form.  

 

I can give you some code to do this (if that's what you need).