Support

Account

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

  • 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;
    }