WPPizza – A Restaurant Plugin for WordPress › Support › General Support › Restrict by Street Address
- AuthorPosts
- 29 March, 2018 at 4:35 am #36355
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.'); } });
29 March, 2018 at 1:29 pm #36392i would suggest you create validation rules like mentioned here
https://docs.wp-pizza.com/developers/?section=additional-validation-function30 March, 2018 at 3:59 am #36411Hi Olly,
Thank you for the reference, I’ve never used that plugin before.
Could you please offer some more assistance?
30 March, 2018 at 4:09 am #36412it’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-functions30 March, 2018 at 4:29 am #36413am 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)30 March, 2018 at 4:58 am #36414Thanks 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!
30 March, 2018 at 5:01 am #36415@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
30 March, 2018 at 1:30 pm #36429>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.asp30 March, 2018 at 1:34 pm #36430PS: 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 observation31 March, 2018 at 3:55 am #36442Hi 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 CashCould you give me a point in the right direction? Thanks again!
31 March, 2018 at 4:01 am #36443https://docs.wp-pizza.com/developers/?section=gateway-filter-frontend
in conjunction withhttps://docs.wp-pizza.com/developers/?section=global-wppizza-functions
->WPPIZZA_IS_PICKUPi would have thought
31 March, 2018 at 5:26 am #36444Thanks Olly!
- AuthorPosts
- The topic ‘Restrict by Street Address’ is closed to new replies.