2

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 !

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.