if (empty($val) === false) {
        $phoneVal = $val;
        $foundMatches = preg_match('/^\\(?(\\d{' . $var[0] . '})\\)?[- ]?(\\d{' . $var[1] . '})[- ]?(\\d{' . $var[2] . '})$/', $phoneVal);
        if ($foundMatches !== 1) {
            return false;
        }
    }
    return true;
};
$rule->setCustomRuleValidateFunction($func);
//Finally, add the rule to the form
$a_formRules[] = $rule;
//CREATE FORM AND SETUP
$o_form = new JsonFormBuilder($modx, 'contactForm');
$o_form->setRedirectDocument(3);
$o_form->addRules($a_formRules);
//SETUP EMAIL
//Note, this is not required, you may want to not send an email and record the data to a database.
$o_form->setEmailToAddress($modx->getOption('emailsender'));
$o_form->setEmailFromAddress('*****@*****.**');
$o_form->setEmailFromName('No One');
$o_form->setEmailSubject('JsonFormBuilder Custom Validate Test');
//Set jQuery validation on and to be output
$o_form->setJqueryValidation(true);
//You can specify that the javascript is sent into a placeholder for those that have jquery scripts just before body close. If jquery scripts are in the head, no need for this.
$o_form->setPlaceholderJavascript('JsonFormBuilder_myForm');
//ADD ELEMENTS TO THE FORM IN PREFERRED ORDER
$o_form->addElements(array($o_fe_phone, $o_fe_buttSubmit));
//The form HTML will now be available via
//$o_form->output();
//This can be returned in a snippet or passed to any other script to handle in any way.
//Same setup for a radio group
$r = new FormRule(FormRuleType::required, $o_fe_catname, NULL, 'As you have a cat, please tell us its name.');
$r->setCondition(array($o_fe_havecat, 'Yes'));
$a_formRules[] = $r;
$r = new FormRule(FormRuleType::conditionShow, $o_fe_catname);
$r->setCondition(array($o_fe_havecat, 'Yes'));
$a_formRules[] = $r;
$o_fe_buttSubmit = new JsonFormBuilder_elementButton('submit', 'Submit Form', 'submit');
//Set required fields
$a_formFields_required = array($o_fe_havedog, $o_fe_havecat, $o_fe_name, $o_fe_email);
foreach ($a_formFields_required as $field) {
    $a_formRules[] = new FormRule(FormRuleType::required, $field);
}
//Make email field require a valid email address
$a_formRules[] = new FormRule(FormRuleType::email, $o_fe_email, NULL, 'Please provide a valid email address');
//CREATE FORM AND SETUP
$o_form = new JsonFormBuilder($modx, 'contactForm');
$o_form->setRedirectDocument(3);
$o_form->addRules($a_formRules);
$o_form->setEmailToAddress($modx->getOption('emailsender'));
$o_form->setEmailFromAddress($o_form->postVal('email_address'));
$o_form->setEmailFromName($o_form->postVal('name_full'));
$o_form->setEmailSubject('JsonFormBuilder Contact Form Submission - From: ' . $o_form->postVal('name_full'));
$o_form->setEmailHeadHtml('<p>This is a response sent by ' . $o_form->postVal('name_full') . ' using the contact us form:</p>');
$o_form->setJqueryValidation(true);
$o_form->setPlaceholderJavascript('JsonFormBuilder_myForm');
//ADD ELEMENTS TO THE FORM IN PREFERRED ORDER
$o_form->addElements(array($o_fe_name, $o_fe_email, $o_fe_havedog, $o_fe_dogname, $o_fe_havecat, $o_fe_catname, $o_fe_buttSubmit));
//The form HTML will now be available via
//$o_form->output();
//This can be returned in a snippet or passed to any other script to handle in any way.
/*CREATE FORM AND ADD ELEMENTS*/
/*----------------------------*/
$o_form = new JsonFormBuilder($modx, 'myContactForm');
$o_form->setRedirectDocument(3);
$o_form->addRules($a_formRules);
//Specify to and from email addresses, also see replyTo, CC and BCC options.
$o_form->setEmailToName('To Name');
$o_form->setEmailToAddress($modx->getOption('emailsender'));
//You can set CC or BCC options
//$o_form->setEmailCCAddress('*****@*****.**');
//$o_form->setEmailBCCAddress('*****@*****.**');
//or you can use an array of addresses like so.
//$o_form->setEmailToAddress(array('*****@*****.**','*****@*****.**'));
$o_form->setEmailFromAddress($o_form->postVal('email_address'));
$o_form->setEmailFromName($o_form->postVal('name_full'));
$o_form->setEmailSubject('MyCompany Contact Form Submission - From: ' . $o_form->postVal('name_full'));
$o_form->setEmailHeadHtml('<p>This is a response sent by ' . $o_form->postVal('name_full') . ' using the contact us form:</p>');
$o_form->setJqueryValidation(true);
$o_form->setPlaceholderJavascript('JsonFormBuilder_myForm');
//Set extra classes on your form elements (adds to the wrapper and the inner element).
$a_els = array($o_fe_name, $o_fe_age, $o_fe_dob, $o_fe_attend, $o_fe_username, $o_fe_email, $o_fe_userPass, $o_fe_userPass2, $o_fe_address, $o_fe_city, $o_fe_usstates, $o_fe_postcode, $o_fe_staff);
foreach ($a_els as $e) {
    $e->setExtraClasses(array('half'));
}
$a_els = array($o_fe_company, $o_fe_companyPhone, $o_fe_employees, $o_fe_website);
foreach ($a_els as $e) {
    $e->setExtraClasses(array('quart'));
}
//Add elements to output along with any HTML as a string element.
$o_form->addElements(array($o_fe_userGroup, '<h2>Personal Information</h2>', $o_fe_name, $o_fe_age, $o_fe_dob, $o_fe_attend, $o_fe_username, $o_fe_email, '<h2>Password</h2>', $o_fe_userPass, $o_fe_userPass2, '<h2>Address</h2>', $o_fe_address, $o_fe_city, $o_fe_usstates, $o_fe_postcode, '<h2>Company Information</h2>', $o_fe_company, $o_fe_companyPhone, $o_fe_employees, $o_fe_website, '<h2>Performance</h2>', $o_fe_staff, $o_fe_foodprefer, '<h2>Matrix/Group Elements</h2>', $o_fe_checkMatrix, $o_fe_radioMatrix, $o_fe_textMatrix, '<h2>Attach your Resume and Application</h2>', $o_fe_resume, $o_fe_applcation, '<h2>Additional</h2>', $o_fe_notes, $o_fe_checkNews, $o_fe_checkTerms, $o_fe_buttSubmit, $o_fe_buttReset));
//The form HTML will now be available via