Issue with Laravel Rules & Regex (Or) Operator

Issue with Laravel Rules & Regex (OR) operator

http://laravel.com/docs/validation#rule-regex

regex:pattern

The field under validation must match the given regular expression.

Note: When using the regex pattern, it may be necessary to specify rules in an array instead >of using pipe delimiters, especially if the regular expression contains a pipe character.

To clarify:
You would do something like this

$rules = array('test' => array('size:5', 'regex:foo'));

regex:pattern validation in Laravel

See this note in the official documentation:

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.

So, this should work:

'facebook' => ['url', 'regex:/(?:(?:http|https):\/\/)?(?:www.)?facebook.com\/(?(?:\w)*#!\/)?(?:pages\/)?(?:[?\w\-]*\/)?(?:profile.php\?id=(?=\d.*))?([\w\-]*))/', 'nullable']

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.

Regular Expression is not working properly in laravel validation

That is normal.
You need to think about regex like a search engine. With your rule, you actually say:

Is there a string that has 3 numbers and then a hyphen (-)
3 numbers and then a hyphen (-)
3 numbers

So this is true:

123-112-111

But also this is true:

111-111-111111111
145-156-1155
123-456-87897

Because all of them have the 3 numbers and hyphen, 3 numbers and hyphen, 3 numbers and hyphen!

You need to limit your input differently. Maybe with another rule in controller, for example:

'id'  => 'required|regex:/[0-9]{3}-[0-9]{3}-[0-9]{3}/|max:11|unique:info',

This was just a basic example, I am sure you can figure out something even better.

Also check this website: https://regexr.com/



Update

This is actually the best solution for your problem:

/[0-9]{3}-[0-9]{3}-[0-9]{3}$/

With $ at the end of your regex you say:

This is the end of my string, don't accept the input if user writes more than 3 characters after second hyphen.

Laravel pattern validation pipe character issue

The answer, essentially, is that you cannot use a pipe if you're specifying all the rules in one string like you're trying to do. The pull request that m.buettner mentioned was closed. However, Tayler Otwell mentioned an alternative method you can use: specify the rules in an array. An example of this would be:

$rules = array(
'field' => array('size:5', 'match:/foo|bar/')
);

Laravel: Validation alpha and regex pattern on update

Considering the small chat we had in the comments, what you must do is to first get the model from the database and to diff the request with the model's attributes. Then you can keep the validation rules for the changed attributes only.

public function update(int $id, Request $request, User $userRepository)
{
$user = $userRepository->find($id);

$changedAttributes = array_diff($request->all(), $user->getAttributes());

$validationRules = array_intersect_key([
'name' => ['required', 'alpha','min:2', 'max:255'],
'last_name' => ['required', 'alpha','min:2', 'max:255'],
'mobile' => ['required', 'string', 'regex:/\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|
2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|
4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.$setting->id.''],
], $changedAttributes);

$this->validate($request, $validationRules);

$user->update($changedAttributes);

return Redirect::back()->with('success','User updated successfully');
}

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 :)

Laravel Regex Match Url with JPG not working

As mentionned here, Laravel | delimiter is conflicting with the regex alternation |.

The regex considered in

required|regex:/(http|https)...

is /(http, thus missing the ending delimiter.

Seems you can fix the issue by using (I took the liberty to clean a few things in your regex):

'url' => array('required', 'regex:/(https?):\/\/(www\.)?[\w.-]+\.[a-zA-Z]+\/((([\w\/-]+)\/)?[\w.-]+\.(png|gif|jpe?g)$)/ig')

If Laravel is okay with using different delimiters than / I'd also recommend using something else (~ for example) to lighten the forest of \/.



Related Topics



Leave a reply



Submit