Search code examples
djangoformserror-handlingemailfield

django: raise an error similar to EmailField


When using an email field, attempting to submit a form with an invalid email address raises and error like this:

enter image description here

What I like about this:

  • doesn't reload the page, rather doesn't submit at all and instead displays an error

  • the look

Here is what I am able to accomplish currently with Name and Password (CharFields) enter image description here

The "A name is needed!" and "This field is required" messages only display after the form is submitted, which is not as sleek as displaying without the form even needing to be submitted.


Solution

  • The popup that you see for the email it is not django validation. It is the build in in-browser html5 email field checking (and some older browsers will not do it for you).

    And you can't get any kind of validation in html5, but for checking field presents you can add required attribute to the rendered input field.

    There are several ways to add attibutes to widgets in django. take a look to the How do I get Django forms to show the html required attribute? and select appropriate for you.