0
votes

I have a laravel validation rule something like this

public function rules(){
   return [
            'title' => 'required|max:100',
            'featured_image' => 'required|max:100|regex:(\d)+.(?:jpe?g|png|gif)',

        ];
}

I have a txt field where i dynamically add an image name, something like this (8123123123.jpg OR 234234234.png). If the text field doesn't have this pattern i want to show an error.

Now this regex does work in http://regexr.com/ but in laravel it doesn't. So basically it should look for digits as file name and should end with .jpg or .png

Any help is appreciated

2
Something about an unknown modifier + and . I think it might be because of the reason that this regex is for javascriptomer Farooq
Or it might be due to the pipe symbols, as laravel also follows the same pattern to separate different rules.omer Farooq
You need to escape the dot - like \.. Additionally, you might want to make sure, that the jpg/png/gif part is at the very end of the string, so combined, this would be: \.(?:jpe?g|png|gif)$Jan
Here is what i tried ( required|max:100|regex:\.(?:jpe?g|png|gif)$ ) and got an error saying Delimiter must not be alphanumeric or backslashomer Farooq
Try 'featured_image' => array('required', 'max:100', 'regex:/[0-9]+[.](jpe?g|png|gif)/')Wiktor Stribiżew

2 Answers

1
votes

use like this

'featured_image' => ['required', 'max:100', 'regex:/(\d)+.(?:jpe?g|png|gif)/']

You have to add a regex delimiter http://php.net/manual/en/regexp.reference.delimiters.php

0
votes

According to the docs you should put your validations in a array:

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.