How do I get validation error for either invalid email or email field left blank?

radiobrain77

I am trying to validate fields in a form where I want two different messages for two different problems with the input.

I have the following code:

validates_format_of :email, 
                    :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i,
                    :allow_blank => false

If the email field is left blank then the error message "Email is invalid" is shown in the website.

How can I get the validation to return a message saying the field cannot be blank if it is left out by the user, instead of just saying it is too short?

pdobb

I like to handle this with 2 different validations (and make sure they don't both fire at the same time). So something like this:

validates_format_of :email,
                    :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i,
                    :allow_blank => true
validates_presence_of :email

The validates_presence_of handles making sure the email is not blank. And changing validates_format_of to use :allow_blank => true will make sure the formatting validation won't run if the email is blank.

In Rails 3.0+ you can also combine the two validations into a single one using validates:

validates :email,
          presence: true,
          format: { with: /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i,
                    allow_blank: true }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I get validation error for either invalid email or email field left blank?

From Dev

How do I disable CakePHP's client side email validation?

From Dev

How do I suppress jQuery warnings for invalid email address?

From Dev

how to use onkeypress for either email or mobile in the same field?

From Dev

how to validate either email or mobile in same input field?

From Dev

How do I extend UserCreationForm to include email field

From Dev

How to show particular textbox border red color if i enter invalid email or blank in javascript?

From Dev

Request repr() unavailable in Django 500 error email: how do I debug how to get the full stack trace?

From Dev

Request repr() unavailable in Django 500 error email: how do I debug how to get the full stack trace?

From Dev

AngularJS validation - email switch and email field

From Dev

AngularJs email validation error

From Dev

Error in validation of an email

From Dev

How to check email validation?What is the error?

From Dev

how to do email validation in Edit text in android

From Dev

How to properly do email validation on user registration?

From Dev

Spring validation: How do I remove leading/trailing whitespace from an email before validation?

From Dev

Django error email is too long. How do I truncate it?

From Dev

How do I get the real email address with Exchange web services?

From Dev

How do I get users with a specific email domain from PostgreSQL?

From Dev

How do I get the real email address with Exchange web services?

From Dev

How do I get the email value using the query string method?

From Dev

How do I get the same formating in my email as on my website?

From Dev

How to verify that email field is not blank before submitting form

From Dev

jquery validation for email allowing blank somehow

From Dev

How do I record the email address of a user that clicks a link in an email?

From Dev

How do I send Email using an email client?

From Dev

How do I send files in an email by using the Python email module?

From Dev

How I can parse email to get original recipient of an email?

From Dev

How do I add phone number & email validation to Kendo-UI Grid?

Related Related

  1. 1

    How do I get validation error for either invalid email or email field left blank?

  2. 2

    How do I disable CakePHP's client side email validation?

  3. 3

    How do I suppress jQuery warnings for invalid email address?

  4. 4

    how to use onkeypress for either email or mobile in the same field?

  5. 5

    how to validate either email or mobile in same input field?

  6. 6

    How do I extend UserCreationForm to include email field

  7. 7

    How to show particular textbox border red color if i enter invalid email or blank in javascript?

  8. 8

    Request repr() unavailable in Django 500 error email: how do I debug how to get the full stack trace?

  9. 9

    Request repr() unavailable in Django 500 error email: how do I debug how to get the full stack trace?

  10. 10

    AngularJS validation - email switch and email field

  11. 11

    AngularJs email validation error

  12. 12

    Error in validation of an email

  13. 13

    How to check email validation?What is the error?

  14. 14

    how to do email validation in Edit text in android

  15. 15

    How to properly do email validation on user registration?

  16. 16

    Spring validation: How do I remove leading/trailing whitespace from an email before validation?

  17. 17

    Django error email is too long. How do I truncate it?

  18. 18

    How do I get the real email address with Exchange web services?

  19. 19

    How do I get users with a specific email domain from PostgreSQL?

  20. 20

    How do I get the real email address with Exchange web services?

  21. 21

    How do I get the email value using the query string method?

  22. 22

    How do I get the same formating in my email as on my website?

  23. 23

    How to verify that email field is not blank before submitting form

  24. 24

    jquery validation for email allowing blank somehow

  25. 25

    How do I record the email address of a user that clicks a link in an email?

  26. 26

    How do I send Email using an email client?

  27. 27

    How do I send files in an email by using the Python email module?

  28. 28

    How I can parse email to get original recipient of an email?

  29. 29

    How do I add phone number & email validation to Kendo-UI Grid?

HotTag

Archive