Element Framework

Email Field

  • HTML
  • JS
  • CSS
<ef-panel spacing>
  <p>Email</p>
  <ef-email-field placeholder="Business email address" icon="email"></ef-email-field>
</ef-panel>

                  
ef-panel {
  max-width: 450px;
}

ef-email-field {
  width: 250px;
}

p {
  margin-bottom: 4px;
}

ef-email-field is a form control to confidently obtain an email input from users.

Basic usage

ef-email-field has similar behaviors to the native email input type.

<ef-email-field placeholder="Business email address"></ef-email-field>

Getting value

The field's value can be accessed directly using the value property.

<ef-email-field
  id="email-input"
  value="awesome@tmail.com"
></ef-email-field>
var emailInput = document.getElementById("email-input");
emailInput.value; // "awesome@tmail.com"

You can also listen for the value-changed event that triggers when the value changes due to user interactions.

  • HTML
  • JS
  • CSS
<ef-email-field id="input" placeholder="Type an email ..."></ef-email-field>
<p>Value: <code id="value-text"></code></p>
var element = document.getElementById("input");
var valueChangedText = document.getElementById('value-text');

element.addEventListener("value-changed", function(e) {
  valueChangedText.innerHTML = e.detail.value;
});

                  
<ef-email-field id="input"></ef-email-field>
<p>Value: <code id="value-text"></code></p>
var element = document.getElementById("input");
var valueChangedText = document.getElementById("value-text");

element.addEventListener("value-changed", function (e) {
  valueChangedText.innerHTML = e.detail.value;
});

Input validation

Validation occurs when the constraints are provided and the value changes. If the error state changes, it will dispatch the error-changed event along with the current error state.

Alternatively, you can check the error property to confirm if the input is valid.

Input length

The maxlength attribute limits the number of characters that users can type into the input and the minlength attribute is used to set the minimum of characters required. ef-email-field will show error styles if the condition is not met.

  • HTML
  • JS
  • CSS
<ef-email-field id="email-input" minlength="8" maxlength="14" placeholder="Length between 8 to 14 characters"></ef-email-field>
<p id="error-text"></p>
var element = document.getElementById("email-input");
var errorChangedText = document.getElementById('error-text');
element.addEventListener("error-changed", function(e) {
  if (e.detail.value) {
    errorChangedText.innerHTML = "Must be in standard email format with between 8-14 characters.";
  } else {
    errorChangedText.innerHTML = "";
  }
});
#error-text {
  color: #d94255;
}

ef-email-field {
  width: 275px;
}
<ef-email-field
  id="email-input"
  minlength="8"
  maxlength="14"
></ef-email-field>
<p id="error-text"></p>
var element = document.getElementById("email-input");
var errorChangedText = document.getElementById("error-text");
element.addEventListener("error-changed", function (e) {
  if (e.detail.value) {
    errorChangedText.innerHTML =
      "Must be in standard email format with between 8-14 characters.";
  } else {
    errorChangedText.innerHTML = "";
  }
});

Pattern

You can use a regular expression to validate the input value by adding the pattern attribute.

  • HTML
  • JS
  • CSS
<ef-email-field id="email" pattern=".+@mail.com" placeholder="Type email ending with &apos;@mail.com&apos;"></ef-email-field>
<p id="error-text"></p>
var element = document.getElementById("email");
var errorChangedText = document.getElementById("error-text");
element.addEventListener("error-changed", function(e) {
  if (e.detail.value) {
    errorChangedText.innerHTML = "Email must end with '@mail.com'.";
  } else {
    errorChangedText.innerHTML = "";
  }
});
#error-text {
  color: #d94255;
}

ef-email-field {
  width: 275px;
}
<ef-email-field id="email" pattern=".+@mail.com"></ef-email-field>
<p id="error-text"></p>
var element = document.getElementById("email");
var errorChangedText = document.getElementById("error-text");
element.addEventListener("error-changed", function (e) {
  if (e.detail.value) {
    errorChangedText.innerHTML = "Email must end with '@mail.com'.";
  } else {
    errorChangedText.innerHTML = "";
  }
});

Icon

An inline icon can be displayed inside the input using icon.

  • HTML
  • JS
  • CSS
<ef-email-field icon="individual" placeholder="Add your personal email .."></ef-email-field>

                  
ef-email-field {
  width: 275px;
}
<ef-email-field icon="individual"></ef-email-field>

An icon can become actionable by adding the icon-has-action attribute to the element, and ef-email-field will fire the icon-click event when a user clicks on the icon. You can add an event listener to this event to execute your code.

  • HTML
  • JS
  • CSS
<ef-email-field id="email-list-input" placeholder="Type email and then click the icon ..." icon="msgr-adduser" icon-has-action pattern="^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$"></ef-email-field>
<p id="error-text"></p>
<p id="email-added"></p>
var element = document.getElementById('email-list-input');
var emailList = document.getElementById('email-added');
var errorChangedText = document.getElementById("error-text");

element.addEventListener('icon-click', function(e) {
  if (!element.error && element.value.length > 0) {
    emailList.innerHTML = element.value + " is added.";
  }
});
element.addEventListener("error-changed", function(e) {
  if (e.detail.value) {
    errorChangedText.innerHTML = "Invalid email format.";
    emailList.innerHTML = "";
  } else {
    errorChangedText.innerHTML = "";
  }
});
#error-text {
  color: #d94255;
}

ef-email-field {
  width: 275px;
}
<ef-email-field
  id="email-list-input"
  icon="msgr-adduser"
  pattern="^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$"
  icon-has-action
></ef-email-field>
<p id="email-added"></p>
var element = document.getElementById("email-list-input");
var emailList = document.getElementById("email-added");

element.addEventListener("icon-click", function (e) {
    if (!element.error && element.value.length > 0) {
      emailList.innerHTML = element.value + " is added.";
    }
});

API Reference

Attributes

string
pattern
Set regular expression for input validation
string
placeholder
Set placeholder text
boolean
error
Set state to error
boolean
warning
Set state to warning
boolean
transparent
Disables all other states and border/background styles. Use with advanced composite elements requiring e.g. multi selection in combo-box when parent container handles element states.
number | null
maxlength
Set character max limit
number | null
minlength
Set character min limit
boolean
multiple
Set to multiple mode, allows multiple emails in a single input
string
icon
Specify icon to display in input. Value can be icon name
boolean
icon-has-action
Specify when icon need to be clickable
string
value
Input's value
boolean
readonly
Set readonly state
boolean
disabled
Set disabled state

Properties

string
pattern
Set regular expression for input validation
""
string
placeholder
Set placeholder text
""
boolean
error
Set state to error
false
boolean
warning
Set state to warning
false
boolean
transparent
Disables all other states and border/background styles. Use with advanced composite elements requiring e.g. multi selection in combo-box when parent container handles element states.
false
number | null
maxLength
Set character max limit
number | null
minLength
Set character min limit
boolean
multiple
Set to multiple mode, allows multiple emails in a single input
false
string
icon
Specify icon to display in input. Value can be icon name
""
boolean
iconHasAction
Specify when icon need to be clickable
false
string
value
Input's value
""
boolean
readonly
Set readonly state
false
boolean
disabled
Set disabled state
false

Methods

select()
Select the contents of input

Events

value-changed
Dispatched when value changes
error-changed
Dispatched when error state changes
icon-click
Dispatched only when element has icon-has-action attribute and icon is clicked