Wordpress – Ganga https://gangaramdas.com Learn how to Build Awesome Websites & Create a Web Design Business Fri, 24 Oct 2025 08:31:38 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.4 https://gangaramdas.com/wp-content/uploads/2020/06/cropped-small-favio-32x32.png Wordpress – Ganga https://gangaramdas.com 32 32 How to Add Phone Number Validation in WPForms https://gangaramdas.com/how-to-add-phone-number-validation-in-wpforms/ https://gangaramdas.com/how-to-add-phone-number-validation-in-wpforms/#respond Fri, 24 Oct 2025 08:31:28 +0000 https://gangaramdas.com/?p=218492 This blog is about how to validate phone number fields in WPForms.
By adding this method, you can make sure users enter only valid phone numbers such as 10-digit formats.
It helps in maintaining correct data for leads or contact forms.

 

/**
* Add phone number validation in WPForms
*/
function wpforms_custom_phone_validation( $field_id, $field_submit, $form_data ) {

// Optional: Change field ID if needed (1 = Phone field ID)
if ( $field_id == ‘1’ && !empty( $field_submit ) ) {

// Allow only 10 digit numbers
if ( !preg_match( ‘/^[0-9]{10}$/’, $field_submit ) ) {
wpforms()->process->errors[ $form_data[‘id’] ][ $field_id ] = esc_html__( ‘Please enter a valid 10-digit phone number.’, ‘wpforms’ );
}
}
}
add_action( ‘wpforms_process_validate’, ‘wpforms_custom_phone_validation’, 10, 3 );

]]>
https://gangaramdas.com/how-to-add-phone-number-validation-in-wpforms/feed/ 0
How to Add Email Field Validation in Wpforms https://gangaramdas.com/how-to-add-email-field-validation-in-wpforms/ https://gangaramdas.com/how-to-add-email-field-validation-in-wpforms/#respond Tue, 21 Oct 2025 06:15:10 +0000 https://gangaramdas.com/?p=218473 Adding email field validation in WPForms (WordPress plugin) can be done in a few different ways — depending on what kind of validation you need (basic, custom, or conditional).

Here’s a step-by-step guide 👇

1. Basic Email Validation (Built-in)

WPForms automatically validates the email field.

Steps:

Go to your WordPress Dashboard → WPForms → All Forms.

Edit your form.

Add an Email field from the Add Fields panel (or use the one already in your form).

Click on the Email field to open its settings.

Ensure “Required” is checked (so users can’t leave it blank).

WPForms automatically ensures:

Email contains “@” and a valid domain (e.g., example@gmail.com).

Invalid emails will show an error like:
“Please enter a valid email address.”

 2. Add Custom Email Validation (e.g., block specific domains)

If you want custom logic (e.g., no Yahoo or Gmail emails), you can add a small PHP snippet.

Add this code to your functions.php or a custom plugin:

function custom_wpforms_email_validation( $field_id, $field_submit, $form_data ) {
// Only apply to specific form and field (optional)
if ( absint( $form_data[‘id’] ) === 123 && $field_id === ‘1’ ) {
$email = sanitize_email( $field_submit );
$blocked_domains = array( ‘yahoo.com’, ‘gmail.com’ );
$domain = substr( strrchr( $email, “@” ), 1 );

if ( in_array( strtolower( $domain ), $blocked_domains, true ) ) {
wpforms()->process->errors[ $form_data[‘id’] ][ $field_id ] = esc_html__( ‘Sorry, emails from this domain are not allowed.’, ‘wpforms’ );
}
}
}
add_action( ‘wpforms_process_validate_email’, ‘custom_wpforms_email_validation’, 10, 3 );

What this does:

Validates the email field before form submission.

If the email domain matches a blocked list, it shows a custom error message.

Change:

123 → your form ID.

‘1’ → your field ID.

Add/remove domains in $blocked_domains.

 3. Advanced Validation Options

You can also:

Use Regex (regular expressions) to enforce patterns (e.g., company emails only).

Use Conditional Logic (in Pro version) to show/hide other fields based on the email.

Example (Regex for company domain only)

function wpforms_company_email_validation( $field_id, $field_submit, $form_data ) {
if ( absint( $form_data[‘id’] ) === 123 && $field_id === ‘1’ ) {
if ( ! preg_match( ‘/@company\.com$/i’, $field_submit ) ) {
wpforms()->process->errors[ $form_data[‘id’] ][ $field_id ] = esc_html__( ‘Please use your company email address.’, ‘wpforms’ );
}
}
}
add_action( ‘wpforms_process_validate_email’, ‘wpforms_company_email_validation’, 10, 3 );

 

]]>
https://gangaramdas.com/how-to-add-email-field-validation-in-wpforms/feed/ 0
How to Add Coupon Code Field Validation on wpforms https://gangaramdas.com/how-to-add-coupon-code-field-validation-on-wpforms/ https://gangaramdas.com/how-to-add-coupon-code-field-validation-on-wpforms/#respond Mon, 31 Oct 2022 08:51:03 +0000 https://gangaramdas.com/?p=218149 This blog is about how to validate text fields which can be used as coupon codes or any standard method and even with this method you can even control spam entries.

Paste the below code to the fincation.php file and I will recommend first creating a child theme and then pasting the code into the child theme function.php file.

/**
 * Add coupon code field validation or text field validation.
 *
 */
 
function wpf_dev_validate_coupon( $fields, $entry, $form_data ) {
       
    // Optional, you can limit to specific forms. Below, we restrict output to
    // form #164.
    if ( absint( $form_data[ 'id' ] ) !== 17177 ) {
        return $fields;
    }
     
    //get the value of the coupon code field the user entered
    $coupon = $fields[8][ 'value' ];
     
    //coupon code array, each coupon separated by comma
    $coupon_code_list = array( 
        'Code1', 
        'Code2',
		'Code3',
	
    );
     
    // check if value entered is not in the approved coupon list array      
    if (!in_array($coupon, $coupon_code_list)) {  
            // Add to global errors. This will stop form entry from being saved to the database.
            // Uncomment the line below if you need to display the error above the form.
            // wpforms()->process->errors[ $form_data[ 'id' ] ][ 'header' ] = esc_html__( 'Some error occurred.', 'plugin-domain' );    
   
            // Check the field ID 5 and show error message at the top of form and under the specific field
               wpforms()->process->errors[ $form_data[ 'id' ] ] [ '8' ] = esc_html__( 'Coupon code not found, please confirm the code and try again.', 'plugin-domain' );
   
            // Add additional logic (what to do if error is not displayed)
        }
    }
add_action( 'wpforms_process', 'wpf_dev_validate_coupon', 10, 3 );

]]>
https://gangaramdas.com/how-to-add-coupon-code-field-validation-on-wpforms/feed/ 0