1

I'm trying to write a validation check in PHP Laravel for a username field with the functionality to let the user know what went wrong. I have a couple of if statements with regular expression checks but it won't work. The requirements of the regular expression are: can't start with a ".", No more than 1 "." in a row, No capitals, Only a-z, No special characters. So for example like this "user.name" would be valid, but things like "username." or ".username" would all be invalid. So far I got this:

 $oValidator = Validator::make(Input::all(), [
            'username' => 'required|regex:/^[a-zA-Z0-9][\w\.]+[a-zA-Z0-9]$/',
            'username' => 'required',
            'password' => 'required',
            'firstname' => 'required',
            'lastname' => 'required',
            'email' => 'required|email'
        ]);

I want to give feedback for the mistakes that user makes, example: user input is ".username", program feedback should be "Dot in front of string is not allowed".

2
  • I need to let the user know where and what went wrong. Also I can use this line if (!preg_match("/^[a-z]+[.]?[a-z]+$/", $name)) { echo "some msg"} but then again the user won't know what went wrong. Commented Mar 1, 2016 at 12:42
  • Thank you for your help, I have tried this and this is working fine: 'username' => 'required|regex:/^[a-zA-Z0-9][\w\.]+[a-zA-Z0-9]$/', but I want to show the users what went wrong if they made a mistake, like if they insert ".username" that the program shows a notification to the user that beginning of a string with "." is not allowed Commented Mar 1, 2016 at 13:29

1 Answer 1

2

All you have to do is to include a custom message for your validation.

 $this->validate($request, [
        'username' => 'required|regex:/^[a-zA-Z0-9][\w\.]+[a-zA-Z0-9]$/',
    ], ['regex' => 'Username cannot start with period(.), etc...]);

Your code should look like this. Please remember regex custom message will apply too all of these fields instead of just username so I would separate username validation like above.

$oValidator = Validator::make(Input::all(), [
        'username' => 'required|regex:/^[a-zA-Z0-9][\w\.]+[a-zA-Z0-9]$/',
        'username' => 'required',
        'password' => 'required',
        'firstname' => 'required',
        'lastname' => 'required',
        'email' => 'required|email'
    ], ['regex' => 'Username cannot start with period, etc...']);
Sign up to request clarification or add additional context in comments.

1 Comment

Desperation made me do this: if (!preg_match('/^[a-z][\w\.]+[a-z]$/', $name)) { $nameErr = "Invalid username, username can not start with a dot \".\", username can not contain capitals and no special characters."; } But your answer is worked perfectly.

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.