Custom Validation in WooCommerce Checkout Form
Custom Validation in WooCommerce Checkout Form
HOME
WOOCOMMERCE CUSTOM VALIDATION IN WOOCOMMERCE CHECKOUT FORM
FOLLOW US
Stay updated via social channels
AMAN MEHRA
JUNE 22, 2021
LEAVE A COMMENT
Get New Post By Email
Tweet on Twitter
Share on Facebook
Name
SUBSCRIBE
RECENT POSTS
Custom Validation in
Yes! custom validation for WooCommerce checkout form as you read in the WooCommerce Checkout Form
title.
How to Re-Order WordPress Posts
By Custom Field?
We can make our own validation rules for each checkout field. If you are familiar
How to Add Title Attribute in
with woocommerce’s action/hook then you can make easily custom validation
WordPress Menu?
rules.
You have to just add the action hook function and apply the custom condition
CATEGORIES
with custom validation rules using the preg_match(), is_numeric(), filter_var(),
and more. You can use and make as per your requirements. How To
Laravel
So, let’s start the tutorial about how you can apply custom validation rules on
PHP
woocommerce checkout form.
Python
ReactJS
Table of Contents
1
WooCommerce Checkout Form Custom Validation WooCommerce
1.1
Custom Validation on First Name and Last Name WordPress
1.2
Custom Validation on Phone Number Field
1.3
Custom Validation Rules on Website URL Field
2
Remove/Unset the Default Validation Rules From the Field MOST VIEWED POSTS
Custom Validation on First Name and Last Name Upload Multiple Featured Images
in a Post OR Page
In this example, I’ll show you can validate for the First Name and Last Name
How can I Prevent SQL Injection in
only contains letters. PHP?
The above example will check if the first name and last name do not contain the
number and I used the ctype_alpha() PHP function to check the fields
string only has letters. See the image below.
Add the below code in your functions.php file. It will add the woocommerce-
invalid class to parent wrapper (.form-row class) of input field. This class will
change the highlighted color to red if fields are incorrect and showing the errors
for them.
This javascript code will run on the blur or on change of the specific field and
check if the field passes the validation or not. If it throws the error then it will
highlight that input field with red color, you can see the below image.
You can also make this validation rule with preg_match() or any other function
that is perfect with your requirements.
preg_match() example:
We will validate the Phone Number if it is more than 10 digits or not. If it will be
more than 10 digits then it will show the error. You can make more validation
rules as per your requirements and also can change the limit of 10 digits to
anything.
If you have a custom field in the woocommerce checkout form like URL, then
you can also validate it with your custom validation rules.
It will show the error message if you enter the incorrect website URL. See the
image below.
You can also remove the default checkout field validation rules.
To remove the default validation rule, you have to just hit the filter hook function
to unset the specific field’s validate parameter. We will use the
woocommerce_checkout_fields hook to achieve this.
In the above code, we have unset the validation rules for the billing fields
Postcode and Email.
Tweet on Twitter
Share on Facebook
LEAVE A REPLY
Your email address will not be published. Required fields are marked *
Comment
Save my name, email, and website in this browser for the next time I comment.
POST COMMENT
Your Blog Coach is the best site Blog How to Change Place Order
Name
for finding the solution to any Button Text in WooCommerce?
WordPress
issue related to coding and learn
How to Implement Multiple Your email address
more cool stuff and tricks. WooCommerce
Constructors For Class in Python?
Contact
Custom Validation in SUBSCRIBE
WooCommerce Checkout Form