Skip Navigation

[Resolved] I want to prevent someone entering an email address as their first name

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a CRED Create User form that includes the first_name field. I would like to prevent Users from submitting an email address in this field.

Solution: The only way to implement this type of validation is to use PHP code on the backend and the cred_form_validate API. Then you could set up a custom regex or other string validation system to test the first name entered and return an error if necessary. Here's a simple example using the validation API to test the first name value:

add_filter('cred_form_validate','no_email_name_validation',10,2);
function no_email_name_validation($error_fields, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$error_fields;
    //uncomment this if you want to print the field values
    //print_r($fields);
    //validate if specific form
    if ($form_data['id']==12345)
    {
        //check first name value
        if ($fields['wpcf-first_name']['value']!='John')
        {
            //set error message for my_field
            $errors['wpcf-first_name']='Only members named John are allowed'';
        }
    }
    //return result
    return array($fields,$errors);
}

Replace 12345 with the numeric ID of your CRED user form, and modify the conditional using your own custom validation code.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
https://stackoverflow.com/questions/12026842/how-to-validate-an-email-address-in-php

This support ticket is created 6 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 1 reply, has 2 voices.

Last updated by Christian Cox 6 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#612484

Tell us what you are trying to do?
I want to prevent someone entering an email address as their first name

Is there any documentation that you are following?
I have been looking at: https://toolset.com/documentation/user-guides/cred-user-forms/ and exploring the use of generic fields, however I cannot see how the "validate_format":1, is used, and how I can prevent users from using an email address as their first name.

Is there a similar example that we can see? Not that I'm aware of.

What is the link to your site?
hidden link (the create user form at the bottom)

#612551

Hi, the validate_form attribute is only helpful if your field is something other than plain text - like an email address or a number. Since the first name field can capture any kind of plain text, there's not really a way to specify additional validation in the field settings. The only way to implement this type of validation is to use PHP code on the backend and the cred_form_validate API. Then you could set up a custom regex or other string validation system to test the first name entered and return an error if necessary. Here's a simple example using the validation API to test the first name value:

add_filter('cred_form_validate','no_email_name_validation',10,2);
function no_email_name_validation($error_fields, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$error_fields;
    //uncomment this if you want to print the field values
    //print_r($fields);
    //validate if specific form
    if ($form_data['id']==12345)
    {
        //check first name value
        if ($fields['wpcf-first_name']['value']!='John')
        {
            //set error message for my_field
            $errors['wpcf-first_name']='Only members named John are allowed'';
        }
    }
    //return result
    return array($fields,$errors);
}

Replace 12345 with the numeric ID of your CRED user form, and modify the conditional using your own custom validation code. I don't have a good PHP email address regex to offer, but a quick search of the web led me to this one you could try: https://stackoverflow.com/questions/12026842/how-to-validate-an-email-address-in-php

The forum ‘Types Community Support’ is closed to new topics and replies.