1

I am trying to add validation, inside my User model to validation emails using regex.

However, it's spits a dummy out at the first apostrophe.

'email' => 'required|regex:/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/',
user3189734
  • 665
  • 1
  • 9
  • 27

2 Answers2

3

Have you tried the 'email' validation rule?

'email' => 'required|email|unique:users,email'

http://laravel.com/docs/4.2/validation#rule-email

ART GALLERY
  • 540
  • 2
  • 8
  • The OP does not want to use the email rule as stated in [the comments](http://stackoverflow.com/questions/28179259/laravel-4-regex-email-validation#comment44727825_28179259) – lukasgeiter Jan 27 '15 at 22:08
1

As the answer to this question on SO states, there is no simple regular expression to validate an email-address. Using your RegEx could maybe catch valid addresses (although that's just speculation of mine). Using the email-validation-rule would be my first choice.

But you are right, this is just the server side in the first place, if you ignore redirecting users back with input and error messages..

On the client-side, you would have some options. The first one would be to simply rely on the build in browser-validation, by declaring the corresponding input-field as an email-address which you should do anyway:

{{ Form::email($name, $value = null, $attributes = array()) }}

Another, more advanced way would be to create some kind of helper to check the typed input via Ajax using the same validation rule and returning the error messages or sth. similar. This could be an additional route to your Model-Resource for example. This way, you would be stable and consistent.

Community
  • 1
  • 1
nozzleman
  • 9,529
  • 4
  • 37
  • 58