You generally do not need to add additional RegEx validation or tag stripping at the validation stage just to prevent script or tag injection, especially if you’re using Laravel’s ORM (Eloquent) and Blade templating engine.
Here’s why:
- Validation (like
'string','max:255') ensures the data is the correct type and length, but does not sanitize it. - ORM/Eloquent protects against SQL injection by parameterizing queries.
- Blade templates automatically escape output with
{{ $name }}to prevent XSS (Cross-Site Scripting).
You only need to worry about XSS if you output user data without escaping, for example using {!! $name !!} or in JavaScript contexts.
If you want to restrict certain characters (like HTML tags) for business logic reasons, you can add a regex:
'name' => ['required', 'string', 'max:255', 'regex:/^[a-zA-Z\s]+$/']
But for security, escaping output is the key. You do not need to strip tags or use regex just for security if you follow Laravel’s conventions.
Summary:
- Use
'string'and'max:255'for validation. - Use
{{ $name }}in Blade to escape output. - Only add regex/tag stripping if your business logic requires it, not for security.
References: