This script adds a unified validation function to validate user input from the frontend for FiveM. The script is written in JavaScript and can be integrated via the .html file.
Download the latest version and drag the folder into the resources folder of your FiveM server.
To validate all user input, you can simply call the static function of the Validator
class.
Keep in mind that you need to put await
before calling this function.
const result = await Validator.validate({
fieldName: {
rules: "required|string",
value: $('#firstname').val()
}
});
An example response would be the following:
{
"firstname": "First name must be filled in.",
"lastname": "Last name must be filled in."
}
You can add your own rules and customize existing rules as well.
- Open the file located in
src/main.js
. - Find the property
availableRules
(It is at the top of the class) - Copy an existing rule and customize the name as well as the logic to your needs.
Some of our rules require additional arguments to properly validate the field. For example, the max
rule requires a number of characters that the field should have. The rule would look like this max:10
. This means that the field must have at least 10 characters or be equal to or greater than 10. Or if it is an array, it needs at least 10 entries in the array.
Rule name | Requires argument | Argument | Example | Explanation |
---|---|---|---|---|
required | ❌ | Checks if the field has been filled. | ||
string | ❌ | Checks if the field is an string. | ||
numeric | ❌ | Checks if the field only contains numbers. | ||
max | ✅ | number | max:12 | Checks if the field does not exceeds the given value. |
min | ✅ | number | min:12 | Checks if the field isn't lower than the given value. |
in | ✅ | string | in:FiveM,altV,RageMP | Checks if the field is included in the given value. |
array | ❌ | Checks if the field is an array. |
I'm happy to help with any questions or problems. Please open an issue on Github or contact me on Discord: oakyy