7

In the documentation I saw this

string:value

The field under validation must be a string type.

But as I understand (from the scarce explanation + the example given) I must validate with a certain string value. But what I want is to validate simply that the input value contains just letters from the alphabet.

Apparently

'name'  => 'required|string'

won't cut it, because it has to be string:SomeValue

So, what is the correct way, to set a validation rule In the model class that validates against string?

2
  • you need to provide some context Commented Feb 26, 2015 at 23:37
  • @Dagon, what do you mean by context? A certain value to validate with, or what? Commented Feb 26, 2015 at 23:39

3 Answers 3

7

I think what you're looking for is the 'alpha' validation rule Laravel provides. This will ensure the value you are validating contains only alphabetic characters. Docs: http://laravel.com/docs/4.2/validation#available-validation-rules

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

Comments

1

You can use the regex validator for this:

'name'  => ['required', 'regex:/^([a-zA-Z]+)(\s[a-zA-Z]+)*$/']

([a-zA-Z]+): This means that the input must begin with one or more letters.

(\s[a-zA-Z]+)*: This means that after the first word (name) you can add one space with another word (name) & it can be done 0 or more times.

If you want to force the user to input his first name(s) + last name(s) you could change the '*' character to a '+' character.

This might be a late answer but it still might help someone.

Comments

0

Also, you can use

required|min:3|max:100|regex:/^[\p{L}\p{M}\s.\-]+$/u
  • This regular expression pattern is used for validating a string containing Unicode alphabetic characters, including letters, marks, and spaces.
  • The pattern starts with ^ which means the start of the string, followed by [\p{L}\p{M}\s.-]+, which is the character class that matches one or more occurrences of Unicode alphabetic characters (\p{L}), marks (\p{M}), spaces (\s), hyphen (-), and dot (.) characters.
  • The + sign after the character class specifies that it should match at least one occurrence of the characters within the character class.
  • List item

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.