Laravel 5.4 - Validation with Regex
Your rule is well done BUT you need to know, specify validation rules with regex separated by pipeline can lead to undesired behavior.
The proper way to define a validation rule should be:
$this->validate(request(), [
'projectName' =>
array(
'required',
'regex:/(^([a-zA-Z]+)(\d+)?$)/u'
)
];
You can read on the official docs:
regex:pattern
The field under validation must match the given regular expression.
Note: When using the regex / not_regex patterns, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character.
Convert the Custom Regex rule to Laravel validator
After make:rule
command you will see that one file was created under rules folder.
In that file you have to define rules as follows
public function passes($attribute, $value)
{
return preg_match('/(^[0-9]+$)+/', $value);
}
and in your controller you can do something like this
use App\Rules\MobileNo;
$rules = [
'phone' => ['required', new MobileNo],
];
Hope this helps :)
Pass custom parameters in Laravel Validation with regex
You can create a custom rule:
php artisan make:rule CustomRegex
Then update the constructor to support both the regex and description of the format.
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class CustomRegex implements Rule
{
/** @var string $attribute The attribute of we are validating. */
public $attribute;
/** @var string $description The description of the regex format. */
public $description;
/** @var string $regex The regex to validate. */
public $regex;
/**
* Create a new rule instance.
*
* @param string $regex The regex to validate.
* @param string $description The description of the regex format.
* @return void
*/
public function __construct(string $regex, string $description)
{
$this->regex = $regex;
$this->description = $description;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$this->attribute = $attribute;
return preg_match($this->regex, $value);
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return trans('validation.custom.name', [
'attribute' => $this->attribute,
'format' => $this->description
]);
}
}
And then when you validate:
use App\Rules\CustomRegex;
request()->validate([
'name' => [
'required', new CustomRegex('/^\p{Lu}\p{L}+ \p{Lu}\p{L}+$/u', 'The description of your format')
]
]);
Then the output message would look like this:
The name must corespond to the given format: The description of your format!
Regex Validation in Laravel 5.2
Use:
return [
'password' => [
'required',
'confirmed',
'min:8',
'max:50',
'regex:/^(?=.*[a-z|A-Z])(?=.*[A-Z])(?=.*\d)(?=.*(_|[^\w])).+$/',
]
];
Firstly, you do not need to check the confirmation separately. Just use the confirmed
rule.
The expression you were using was invalid, and had nothing to do with what you wanted. I do suggest you do some research on regular expressions.
Due to the fact that the expression shown above uses pipes (|
), you can specify the rules using an array.
Edit: You could also use this expression, which appears to have been tested a little more thoroughly.
/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\d\X])(?=.*[!$#%]).*$/
Laravel special validation for phone numbers and post code
Try this:
return [
'phone' => 'required|regex:/(48)[0-9]{9}/',
'postal_code' => 'required:regex:/[0-9]{2}-[0-9]{3}/'
]
How to validate the file name in Laravel 5.4
If somebody else has the same problem like me. I solved my problem by changing the filename to the current time-stamp instead of using the original filename. That way, I don't need to be worried about the validation of the original filename to be saved in the database.
public function store(Request $request)
{
$this->validate($request,[
'heading'=>'required',
'contentbody'=>'required',
'image'=>['required','image','mimes:jpg,png,jpeg,gif,svg','max:2048']
]);
if($request->hasFile('image')){
$inputimagename= time().'.'.$request->file('image')->getClientOriginalExtension();
$request->image->storeAs('public/upload', $inputimagename);
Post::create([
'heading'=>request('heading'),
'content'=>request('contentbody'),
'image'=>$inputimagename,
]);
}
return redirect('/home');
}
Laravel Regex Validation Error
This one should work
protected $rules = [
'preview_path' => ['required|regex:/^.*\.(jpg|png)$/'],
'slide_path' => ['required|regex:/^.*\.(jp2)$/']
];
Related Topics
Using Mx Records to Validate Email Addresses
Laravel 5.4 - Validation with Regex
In PHP How to Inspect Content of a Zip File Without Extracting Its Content First
Easiest Way to Alternate Row Colors in PHP/Html
Sending Xml Data Using Http Post with PHP
How Is Testing the Registry Pattern or Singleton Hard in PHP
Show/Hide Fields Depending on Select Value
Woocommerce - Get Category for Product Page
MySQL Get a Random Value Between Two Values
How to Upload Files in Laravel Directly into Public Folder
Add Http:// Prefix to Url When Missing
How to Use Composer Packages in Codeigniter
Limiting the Output of PHP's Echo to 200 Characters
PHP Preg_Match (.*) Not Matching Past Line Breaks