Restrict by Street Address

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #36355
    justin
    Participant

      Hi Olly!

      We’re trying to restrict the delivery addresses to street names only. We’ve written a script which works however when someone enters a Street name we don’t deliver to. The form is not stopped using event.preventDefault();

      Do you know how we might stop the form being submitted?

      Thanks in advance!

      
      jQuery(document).ready(function() {
      jQuery('#caddress').on('input', function() {
      var input=jQuery(this);
      var is_name=input.val();
      if (input.val().match(/(John|Smith|David|Luke)/i)) {
      input.removeClass("xxinvalid").addClass("xxvalid");
      } else {
      input.removeClass("xxvalid").addClass("xxinvalid");
      }
      });
      });
      jQuery( "#wppizza-send-order" ).submit(function( event ) {
      if (jQuery( "#caddress" ).hasClass( "xxinvalid" )) {
      event.preventDefault();
      alert ('Please check your address is within our delivery area.');
      } else if (jQuery( "#caddress" ).hasClass( "xxvalid" )) {
      } else {
      event.preventDefault();
      alert ('Please enter an address.');
      }
      });
      
      #36392
      Olly
      Admin & Mod

        i would suggest you create validation rules like mentioned here
        https://docs.wp-pizza.com/developers/?section=additional-validation-function

        #36411
        justin
        Participant

          Hi Olly,

          Thank you for the reference, I’ve never used that plugin before.

          Could you please offer some more assistance?

          #36412
          Olly
          Admin & Mod

            it’s not a plugin (well, it is kind of , but it’s inbuilt so to speak)
            it’s simply a way to add a custom validation rule you will then have available in the validation dropdown in wppizza->order form settings for whatever field you want to apply this to
            i.e

            
            //to add your own rule - let's say 'my_rule'
            add_filter('wppizza_filter_validation_rules','my_function');
            function my_function($validation_rules){
            $validation_rules['my_rule']= array( 'lbl'=> 'My Rule', 'parameters'=>false, 'callback'=>false, 'enabled'=>true);
            return $validation_rules;
            }
            

            will add a “My Rule” to the dropdown

            
            $.validator.methods.my_rule = function (value, element) {
            return this.optional(element) || /^(?:\d+|\d{1,3}(?:[\s\.,]\d{3})+)(?:[\.,]\d+)?$/.test(value);
            }
            

            is the javascript that validates the field that has “My Rule” as validation selected (i.e it should return true or false depending on how you want to validate this)

            and if the concept of filters/actions s new to you:
            https://docs.wp-pizza.com/developers/?section=filters-actions-functions

            #36413
            Olly
            Admin & Mod

              am also a bit puzzled as to why an address should match John|Smith|David|Luke
              furthermore, the == NOTE == part here
              https://www.wp-pizza.com/downloads/wppizza-delivery-by-postcode/
              *might* already be sufficient in what you want to do (but i cannot know the exact requirements of course)

              #36414
              justin
              Participant

                Thanks Olly, I understand now. I’ve added the “My Rule” to the address field.

                I’m getting an error I’m sure it has to do with this line, I’m not exactly sure how to include “match(/(banana|lemon|mango|pineapple)/i)” in the test string.

                return this.optional(element) || test(value).match(/(banana|lemon|mango|pineapple)/i);

                Error:

                
                jquery.validate.min.js?ver=3.2.10:4 Uncaught ReferenceError: test is not defined
                at a.validator.jQuery.validator.methods.my_rule ((index):278)
                at a.validator.check (jquery.validate.min.js?ver=3.2.10:4)
                at a.validator.element (jquery.validate.min.js?ver=3.2.10:4)
                at a.validator.onfocusout (jquery.validate.min.js?ver=3.2.10:4)
                at HTMLTextAreaElement.b (jquery.validate.min.js?ver=3.2.10:4)
                at HTMLFormElement.dispatch (jquery.js?ver=1.12.4:3)
                at HTMLFormElement.r.handle (jquery.js?ver=1.12.4:3)
                at Object.trigger (jquery.js?ver=1.12.4:3)
                at Object.a.event.trigger (jquery-migrate.min.js?ver=1.4.1:2)
                at Object.simulate (jquery.js?ver=1.12.4:3)
                jQuery.validator.methods.my_rule @ (index):278
                check @ jquery.validate.min.js?ver=3.2.10:4
                element @ jquery.validate.min.js?ver=3.2.10:4
                onfocusout @ jquery.validate.min.js?ver=3.2.10:4
                b @ jquery.validate.min.js?ver=3.2.10:4
                dispatch @ jquery.js?ver=1.12.4:3
                r.handle @ jquery.js?ver=1.12.4:3
                trigger @ jquery.js?ver=1.12.4:3
                a.event.trigger @ jquery-migrate.min.js?ver=1.4.1:2
                simulate @ jquery.js?ver=1.12.4:3
                c @ jquery.js?ver=1.12.4:3
                jquery.validate.min.js?ver=3.2.10:4 Uncaught ReferenceError: test is not defined
                at a.validator.jQuery.validator.methods.my_rule ((index):278)
                at a.validator.check (jquery.validate.min.js?ver=3.2.10:4)
                at a.validator.checkForm (jquery.validate.min.js?ver=3.2.10:4)
                at a.validator.form (jquery.validate.min.js?ver=3.2.10:4)
                at HTMLFormElement.<anonymous> (jquery.validate.min.js?ver=3.2.10:4)
                at HTMLFormElement.dispatch (jquery.js?ver=1.12.4:3)
                at HTMLFormElement.r.handle (jquery.js?ver=1.12.4:3)
                

                The functions.php file looks like below:

                
                /*-----------------------php (see also https://docs.wp-pizza.com/developers/?section=filters-actions-functions)-----------------------------------------------*/
                //to add your own rule - let's say 'my_rule'
                add_filter('wppizza_filter_validation_rules','my_function');
                function my_function($validation_rules){
                $validation_rules['my_rule']= array( 'lbl'=> 'My Rule', 'parameters'=>false, 'callback'=>false, 'enabled'=>true);
                return $validation_rules;
                }
                function validation() {?>
                <script>
                /*-----------------------js-----------------------------------------------*/
                // then add the rule like so via wp_footer action hook (or add it to an already existing js file)
                // adjust the validation pattern as appropriate for your validation rule
                jQuery(document).ready(function($){
                jQuery.validator.methods.my_rule = function (value, element) {
                return this.optional(element) || test(value).match(/(banana|lemon|mango|pineapple)/i);
                }
                // adding a custom error message if validation fails instead of the default (not actually tested but should work just the same)
                jQuery.extend(jQuery.validator.messages, {
                my_rule: 'some error message',
                })
                });
                </script>  
                <?php }   
                // ENQUEUE SCRIPTS AND STYLES
                function enqueue_theme_scripts(){
                wp_enqueue_script( 'customjs', get_template_directory_uri() . '/ordervalidation.js', array(), '1.0.0', true );
                add_action( 'wp_footer', 'validation', 50 );	
                }  
                add_action( 'wp_enqueue_scripts', 'enqueue_theme_scripts' );
                

                Thank you!

                #36415
                justin
                Participant

                  @Olly

                  I suggested this extension to our friend, they declined saying they don’t want to deliver to certain streets in the postcode -.-

                  So we plan to add the street names in replacement for John|Smith|David|Luke

                  #36429
                  Olly
                  Admin & Mod

                    >I’m getting an error

                    of course you do. your syntax is wrong (it even tells you where the issue is)
                    https://www.w3schools.com/jsref/jsref_regexp_test.asp

                    #36430
                    Olly
                    Admin & Mod

                      PS: why is there a wp_footer action that prints some js if you are also enqueuing a customjs anyway you could add this to instead ?
                      surely , one of these is not required i would have thought
                      just an observation

                      #36442
                      justin
                      Participant

                        Hi Olly,

                        Thank you so much! ^.^ Your reference to W3C Schools helped perfectly.

                        I’m not very proficient with Javascript yet.

                        For anyone else who may need this:
                        return this.optional(element) || /(banana|lemon|mango|pineapple)/i.test(value);

                        BTW, my friend would like to have these rules for payment/pickup/delivery:
                        Delivery: Credit Card only (We’ll be using your stripe extension)
                        Pickup: Credit Card or Cash

                        Could you give me a point in the right direction? Thanks again!

                        #36443
                        Olly
                        Admin & Mod
                          #36444
                          justin
                          Participant

                            Thanks Olly!

                          Viewing 12 posts - 1 through 12 (of 12 total)
                          • The topic ‘Restrict by Street Address’ is closed to new replies.