form_email()
function form_email(string $name, ?string $value = null, array $attributes = []): string
Description
Generates an HTML email input field with type="email". Provides built-in browser email validation.
Parameters
| Parameter | Type | Description |
|---|---|---|
| $name | string | The name attribute for the input element. |
| $value | string|null | (optional) The email address value. |
| $attributes | array | (optional) HTML attributes for the input element. Defaults to an empty array ([]). |
Return Value
| Type | Description |
|---|---|
| string | An HTML email input element. |
Example #1: Basic Email Input
PHP
echo form_email('contact_email', '[email protected]');
// Output:
// <input type="email" name="contact_email" value="[email protected]">Example #2: Email Input with Validation Attributes
PHP
$attributes = [
'placeholder' => '[email protected]',
'required' => 'required',
'multiple' => 'multiple'
];
echo form_email('email_addresses', '', $attributes);
// Output:
// <input type="email" name="email_addresses" placeholder="[email protected]" required="required" multiple="multiple">Most browsers provide basic email format validation, but always validate server-side.
- Use the
multipleattribute to allow multiple email addresses separated by commas. - Add
requiredattribute for HTML5 client-side validation. - Browser validation varies; implement server-side validation with PHP's
filter_var($email, FILTER_VALIDATE_EMAIL)or by using thevalidationmodule that is included with Trongate. - For consistent behavior, always validate email addresses on the server before processing.