Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Email Address

Note

Requires the email-address feature.

Validate a string is an RFC-compliant email address using the email_address crate.

#![allow(unused)]
fn main() {
extern crate fortifier;

use fortifier::Validate;

#[derive(Validate)]
struct User {
    #[validate(email_address)]
    email_address: String
}
}

Types

String

Validate the string is an RFC-compliant email address.

Email address

Validate the value is an RFC-compliant email address.

An EmailAddress can be constructed using EmailAddress::new_unchecked or with different options passed to EmailAddress::parse_with_options, so re-validation is required.

Options

allow_display_text

Whether display text is allowed. Defaults to false.

See Options::allow_display_text for details.

extern crate fortifier;

use fortifier::Validate;

#[derive(Validate)]
struct User<'a> {
    #[validate(email_address(allow_display_text = false))]
    email_address: &'a str
}

fn main() {
    let user = User {
        email_address: "simon@example.com"
    };
    assert!(user.validate_sync().is_ok());

    let user = User {
        email_address: "Simon <simon@example.com>"
    };
    assert!(user.validate_sync().is_err());
}

allow_domain_literal

Whether domain literals are allowed. Defaults to true.

See Options::allow_domain_literal for details.

extern crate fortifier;

use fortifier::Validate;

#[derive(Validate)]
struct User<'a> {
    #[validate(email_address(allow_domain_literal = false))]
    email_address: &'a str
}

fn main() {
    let user = User {
        email_address: "simon@localhost"
    };
    assert!(user.validate_sync().is_ok());

    let user = User {
        email_address: "simon@[127.0.0.1]"
    };
    assert!(user.validate_sync().is_err());
}

minimum_sub_domains

The minimum number of domain segments. Defaults to 0.

See Options::minimum_sub_domains for details.

extern crate fortifier;

use fortifier::Validate;

#[derive(Validate)]
struct User<'a> {
    #[validate(email_address(minimum_sub_domains = 2))]
    email_address: &'a str
}

fn main() {
    let user = User {
        email_address: "simon@example.com"
    };
    assert!(user.validate_sync().is_ok());

    let user = User {
        email_address: "simon@localhost"
    };
    assert!(user.validate_sync().is_err());
}