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):
- is_numeric($value) is not true due to the "space" thousand separator, so I tried removing the space but:
- 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 !