Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1 - Tie our validation function to the 'gform_validation' hook
- add_filter('gform_validation_1', 'checkPostcode');
- function checkPostcode (&$toCheck) {
- if (!checkPostcode ($postcode) ) {
- echo 'Invalid postcode <br>';
- }
- // Permitted letters depend upon their position in the postcode.
- $alpha1 = "[abcdefghijklmnoprstuwyz]"; // Character 1
- $alpha2 = "[abcdefghklmnopqrstuvwxy]"; // Character 2
- $alpha3 = "[abcdefghjkpmnrstuvwxy]"; // Character 3
- $alpha4 = "[abehmnprvwxy]"; // Character 4
- $alpha5 = "[abdefghjlnpqrstuwxyz]"; // Character 5
- $BFPOa5 = "[abdefghjlnpqrst]{1}"; // BFPO character 5
- $BFPOa6 = "[abdefghjlnpqrstuwzyz]{1}"; // BFPO character 6
- // Expression for BF1 type postcodes
- $pcexp[0] = '/^(bf1)([[:space:]]{0,})([0-9]{1}' . $BFPOa5 . $BFPOa6 .')$/';
- // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA with a space
- $pcexp[1] = '/^('.$alpha1.'{1}'.$alpha2.'{0,1}[0-9]{1,2})([[:space:]]{0,})([0-9]{1}'.$alpha5.'{2})$/';
- // Expression for postcodes: ANA NAA
- $pcexp[2] = '/^('.$alpha1.'{1}[0-9]{1}'.$alpha3.'{1})([[:space:]]{0,})([0-9]{1}'.$alpha5.'{2})$/';
- // Expression for postcodes: AANA NAA
- $pcexp[3] = '/^('.$alpha1.'{1}'.$alpha2.'{1}[0-9]{1}'.$alpha4.')([[:space:]]{0,})([0-9]{1}'.$alpha5.'{2})$/';
- // Exception for the special postcode GIR 0AA
- $pcexp[4] = '/^(gir)([[:space:]]{0,})(0aa)$/';
- // Standard BFPO numbers
- $pcexp[5] = '/^(bfpo)([[:space:]]{0,})([0-9]{1,4})$/';
- // c/o BFPO numbers
- $pcexp[6] = '/^(bfpo)([[:space:]]{0,})(c\/o([[:space:]]{0,})[0-9]{1,3})$/';
- // Overseas Territories
- $pcexp[7] = '/^([a-z]{4})([[:space:]]{0,})(1zz)$/';
- // Anquilla
- $pcexp[8] = '/^ai-2640$/';
- // Load up the string to check, converting into lowercase
- $postcode = strtolower($toCheck);
- // Assume we are not going to find a valid postcode
- $valid = false;
- // Check the string against the six types of postcodes
- foreach ($pcexp as $regexp) {
- if (preg_match($regexp,$postcode, $matches)) {
- // Load new postcode back into the form element
- $postcode = strtoupper ($matches[1] . ' ' . $matches [3]);
- // Take account of the special BFPO c/o format
- $postcode = preg_replace ('/C\/O([[:space:]]{0,})/', 'c/o ', $postcode);
- // Take acount of special Anquilla postcode format (a pain, but that's the way it is)
- if (preg_match($pcexp[7],strtolower($toCheck), $matches)) $postcode = 'AI-2640';
- // Remember that we have found that the code is valid and break from loop
- $valid = true;
- break;
- }
- }
- // Return with the reformatted valid postcode in uppercase if the postcode was
- // valid
- if ($valid){
- $toCheck = $postcode;
- return true;
- }
- else return false;
- }
- /*gravity dustin*/
- /**
- * Better Pre-submission Confirmation
- * http://gravitywiz.com/2012/08/04/better-pre-submission-confirmation/
- */
- class GWPreviewConfirmation {
- private static $lead;
- function init() {
- add_filter('gform_pre_render', array('GWPreviewConfirmation', 'replace_merge_tags'));
- add_filter('gform_replace_merge_tags', array('GWPreviewConfirmation', 'product_summary_merge_tag'), 10, 3);
- }
- public static function replace_merge_tags($form) {
- $current_page = isset(GFFormDisplay::$submission[$form['id']]) ? GFFormDisplay::$submission[$form['id']]['page_number'] : 1;
- $fields = array();
- // get all HTML fields on the current page
- foreach($form['fields'] as &$field) {
- // skip all fields on the first page
- if(rgar($field, 'pageNumber') <= 1)
- continue;
- $default_value = rgar($field, 'defaultValue');
- preg_match_all('/{.+}/', $default_value, $matches, PREG_SET_ORDER);
- if(!empty($matches)) {
- // if default value needs to be replaced but is not on current page, wait until on the current page to replace it
- if(rgar($field, 'pageNumber') != $current_page) {
- $field['defaultValue'] = '';
- } else {
- $field['defaultValue'] = self::preview_replace_variables($default_value, $form);
- }
- }
- // only run 'content' filter for fields on the current page
- if(rgar($field, 'pageNumber') != $current_page)
- continue;
- $html_content = rgar($field, 'content');
- preg_match_all('/{.+}/', $html_content, $matches, PREG_SET_ORDER);
- if(!empty($matches)) {
- $field['content'] = self::preview_replace_variables($html_content, $form);
- }
- }
- return $form;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement