1

I have a form field (string) for which I'm adding a "space" thousand separator using this script:

 <script type="text/javascript">
// https://stackoverflow.com/questions/19470499/how-to-format-input-box-text-as-i-am-typing-it
    jQuery(document).ready(function($) {
        $("#input_10").on('keyup', function(){
        var n = parseInt($(this).val().replace(/\D/g,''),10);
        $(this).val(n.toLocaleString());
        });
    </script>

So user types : "100000" and it will display "100 000"

Later on, during form submission, I have a PHP statement which verifies if the field contains a numeric value.

Now comes the problem ($value is the value of the formatted field):

  1. is_numeric($value) is not true due to the "space" thousand separator, so I tried removing the space but:
  2. is_numeric(str_replace(' ', '', $value)) is also not true and does not delete the space added by toLocaleString

How can delete the toLocaleString format that has been added in jQuery when doing my field validation in PHP?

EDIT: PHP Code used:

add_filter( 'gform_field_validation_1_10', 'bja_gf_custom_validation10', 10, 4 );
function bja_gf_custom_validation10( $result, $value, $form, $field ) {
    $formatedNumber = str_replace(' ', '', $value);
        if ( $result['is_valid'] && !is_numeric($formatedNumber) ) {
            $result['is_valid'] = false;
            $result['message'] = 'formatedNumber:' . $formatedNumber;
        }
        return $result;
}

Thanks !

  • 1
    `is_numeric(str_replace(" ","","100 000"));` is `true`. Show us your PHP code. – JustBaron Sep 15 '17 at 09:09
  • Yes but the problem is that the toLocaleString probably added a char that is **not** an empty space. If I enter "100000" (which renders as "100 000") and then do a PHP: `ord(substr($value,4,1));` I receive the ascii code 160... – SeriousBen74 Sep 15 '17 at 09:42
  • Maybe take a look at removing ASCII Chars from PHP String: https://stackoverflow.com/a/1176923/715105 – JustBaron Sep 15 '17 at 10:37

2 Answers2

1

ASCII Code 160 = Non-Breaking Space.

Try using this regex to catch any kind of whitespace or invisible separator.

$re = '/\p{Z}/'; // (any kind of whitespace or invisible separator)
$str = '100 000'; // Your form input    
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);    
// Print the entire match result
var_dump($matches);

Once you have your matches/results, you could remove them from your variable(s).

Test it out here: https://regex101.com/r/sdHYmw/1

If it is just the non-breaking space that you want to remove, and your page is encoded with UTF-8, in which case the encoding for a non-breaking space is 0xC2 0xA0...you could also try this:

$YourFormInput = trim($YourFormInput, "\xC2\xA0");
JustBaron
  • 2,319
  • 7
  • 25
  • 37
  • Humm it didn't catch anything. I've even tried replacing directly the chr(160) using this: `$formatedNumber = str_replace(chr(160), "", $value);` But then I end up with the question mark in a diamond char instead of the char 160. – SeriousBen74 Sep 15 '17 at 10:23
  • Maybe take a look at removing ASCII Chars from PHP String: https://stackoverflow.com/a/1176923/715105 – JustBaron Sep 15 '17 at 10:41
0

You have juste to use: preg_replace("/[^\d]+/","",$my_var);