baptiste fischer
baptiste fischer

Reputation: 21

How to exclude Phone number and URL in string with Validation Laravel?

I want to validate a "title" field which does not include a phone number or url. I have tried with regexes and it only works if I type a phone number or url alone

For exemple:

0617859654 --> work, the validation fail

but

test 0617859654 -> do not work, validation does not detect the phone number

here is my code:

$validator = Validator::make($request->all(), [
            'titre' => 'not_regex:/^([0-9\s\-\+\(\)]*)$/|not_regex:/^https:\/\/\w+(\.\w+)*(:[0-9]+)?\/?$/|not_regex:/^http:\/\/\w+(\.\w+)*(:[0-9]+)?\/?$/',
        ]);

the validation therefore does not work totally, how I can prevent the validation if a phone number or a url is in my title field ?

Thanks !

Upvotes: 2

Views: 196

Answers (1)

Bhola Kr. Khawas
Bhola Kr. Khawas

Reputation: 385

My approach for the solution of this problem is to find a matching pattern in the given string. I don't have the exact solution but PHP has a method called Preg_match() or preg_match_all()

First pass your title inside one these method and it will return True or false or even the matched pattern.

preg_match ( string $pattern , string $subject , array &$matches = null , int $flags = 0 , int $offset = 0 ) : int|false

OR

preg_match_all ( string $pattern , string $subject , array &$matches = null , int $flags = 0 , int $offset = 0 ) : int|false|null

To read more at : https://www.php.net/manual/en/function.preg-match.php

Hope you get the idea. Thanks

Upvotes: 1

Related Questions