Support

Account

Home Forums General Issues Removing Non-Numeric Characters from ACF Form

Solving

Removing Non-Numeric Characters from ACF Form

  • Hi,

    I want to prevent users from inputting non-numeric (with the exception of the decimal marker) text into an ACF field. I was first trying to use the “formatting” field within the acf_add_local_field_group function, but it doesn’t seem there is a way to filter them out. I want to do this on a form (server-side and client-side) that has ACF’s fields so that users are prevented from submitting text that is non-numeric.

    The easiest possible way would be if there was a “float” or “currency” type within the “type” variable of the acf_add_local_field_group function, so it’d look something like this:

    ‘money_amount’ => array(
    ‘label’ => ‘Money Amount’,
    ‘type’ => ‘float’,
    )

    Any suggestions?

    Thanks!

  • Number fields allow only numbers and decimal point.

  • I did that but for some reason it made the field disappear. What I really want to do is to strip non-numeric characters before it is submitted.

    I looked at this forum thread that you previously answered before John: https://support.advancedcustomfields.com/forums/topic/limit-field-value-to-letters-only-with-no-numbers/ and it did not work. This is what I did:

    1. I placed this code into the __construct function of the main plugin php file (the php file with the same name as the plugin). This is the code:

    add_filter(‘acf/validate_value/name=sell_price’, ‘allow_only_floats’, 20, 4);
    add_filter(‘acf/validate_value/name=buy_price’, ‘allow_only_floats’, 20, 4);

    function allow_only_floats($valid, $value, $field, $input) {
    if (!$valid) {
    return $valid;
    }
    if (preg_replace(‘/[^0-9.]/’, $value)) {
    return ‘Only numbers are accepted’;
    }
    return $valid;
    }

    2. When I put in non-numeric characters, it was still being saved with those non-numeric characters.
    3. I’m not really sure what the “20” and “4” values mean, but I just copied it from your previous form post.

    Where could I go from here?

  • I don’t understand where you’re putting your code, I’m guessing it’s not placed correctly. Please show me enough code to see exactly how it is added.

    3) 20 represents the priority that the filter runs, 4 represents the number of arguments to be passed.

  • I tried two things:

    1. I put it at the end of my main WordPress functions.php file.
    2. I put it in the __construct function of the plugin file I am working with (the main plugin.php) file. For context, it looks like this. Note: I’ve replaced the actual plugin name with a generic “plugin” name.

    File: plugin.php
    Code:

    
    <?php
    /**
     * @package Plugin
     * @version 1.0.0
     */
    /*
    Plugin Name: Plugin
    Plugin URI: https://plugin.com/
    Description: Plugin Description
    Author:  Me
    Version: 1.0.0
    Author URI:  https://plugin.com/
    */
    
    class plugin
    {
    	public static $version = '';
    
        /**
         * pluginconstructor.
         *
         * The main plugin actions registered for WordPress
         */
        public function __construct()
        {
    
    add_filter(‘acf/validate_value/name=sell_price’, ‘allow_only_floats’, 20, 4);
    add_filter(‘acf/validate_value/name=buy_price’, ‘allow_only_floats’, 20, 4);
    
    function allow_only_floats($valid, $value, $field, $input) {
    if (!$valid) {
    return $valid;
    }
    if (preg_replace(‘/[^0-9.]/’, $value)) {
    return ‘Only numbers are accepted’;
    }
    return $valid;
    }
    
  • Is the function in the class definition or nested in the __construct function? Your code shows it nested in __construct.

    Have you initialized your new class object

    
    new plugin();
    
  • Hi John,

    Yes, the plugin is already initialized. Even when I put that ACF filter within functions.php, it still does not do the validation if I put in non-numeric characters.

  • There are very few reasons that I can see to cause your validation not to work is that this code is never called or it is called after the hook is actually run.

    
    add_filter('acf/validate_value/name=sell_price', 'allow_only_floats', 20, 4);
    add_filter('acf/validate_value/name=buy_price', 'allow_only_floats, 20, 4);
    

    Another possibility is that the names you are using are not the names you need to use. This would be the case if these fields are sub fields in a group, repeater or flexible content field. Have you tried using the field key variation of these hooks?

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.