You’re correct that is_numeric('2.') returns false in PHP, because 2. (with a trailing dot and no decimals) is not considered a valid numeric string. However, is_numeric('2,') returns true in some locales, but this is not reliable and can lead to inconsistent behavior.
Best Solution:
You should normalize the input before validation. If the user enters 2., you can safely treat it as 2.0 (or just 2). This can be handled in PHP, and you don’t need to mess with JavaScript for this.
PHP Solution
You can use a mutator or a custom method in your Livewire component to normalize the value before validation or usage:
public function updatedValue($value)
{
// Replace comma with dot for decimal separator (optional, based on your locale)
$value = str_replace(',', '.', $value);
// If value ends with a dot (e.g. "2."), append a zero
if (preg_match('/^\d+\.$/', $value)) {
$value .= '0';
}
// Now you can safely check if it's numeric
if (is_numeric($value)) {
$this->value = $value;
} else {
// Handle invalid input
$this->value = null;
}
}
Usage:
Bind your input to wire:model="value" and Livewire will automatically call updatedValue() when the input changes.
Explanation
- This code replaces commas with dots (if needed).
- If the value ends with a dot (like 2.), it appends a zero to make it 2.0.
- Now is_numeric() will return true for 2.0.
Optional: JavaScript/Alpine Solution
If you want to handle this on the frontend with Alpine.js, you can do something similar:
<input
type="text"
x-data
@input="
if ($el.value.match(/^\d+\.$/)) {
$el.value += '0';
}
"
wire:model="value"
/>
But it’s usually better to handle this normalization in PHP, as shown above, to ensure consistency regardless of how the value is set.
Summary:
Normalize the value in PHP by appending a zero if the value ends with a dot. This way, 2. becomes 2.0 and passes is_numeric().
Let me know if you need a more detailed example!