How to validate different types of float in Laravel?

Validating a user’s data is one of the most important aspects of Laravel application development. There are various types of integer validations as stated in the validation rules list in our previous tutorial. There are different types of float values. The larger ones (8 bytes) are often referred to as double data type and the (4 bytes) floating point values. Let’s see what validation rules best fit each of thee types.

We can create validation rules in the Controller class of our Laravel project. These rules check values before we save or store user data in our application and the database. We can create different validation rules for different types of inputs from a user.

There can be separate rules for mobile numbersmaximum character validations, and image and video uploads. For example, checking image dimensions. Let’s check the rule to validate different types of float.

Validation rule for different types of float

We should use the numeric validation rule for float validation.

For example, as shown below.

   $request->validate([
    'float-number' => 'numeric',
   ]);

The rule above checks if the. number. entered supports float value inputs.

However, we can also use the regex patterns for float values that have decimal points. For example prices under a certain format. We can create a regular expression as shown below.

   $rules = array(
            'price_value' => 'regex:/^\d*(\.\d{2})?$/'
    );

The rules above can be applied to all different types of float values.

Leave a Comment