Skip to content

Import

import { Field } from '@dnb/eufemia/extensions/forms'
render(<Field.Name />)
render(<Field.Name.First />)
render(<Field.Name.Last />)
render(<Field.Name.Company />)

Description

Field.Name is a wrapper component for the input of strings, with user experience tailored for first name, last name and company names.

There is a corresponding Value.Name component.

import { Field, Form } from '@dnb/eufemia/extensions/forms'
function MyForm() {
return (
<Form.Handler>
<Field.Name />
<Field.Name.First value="Nora" />
<Field.Name.Last value="Mørk" />
<Field.Name.Company value="DNB" />
</Form.Handler>
)
}

Relevant links

Sources

Characteristics

Allowed characters

  • Field.Name.First and Field.Name.Last: Only letters, hyphens, and spaces are allowed.
  • Field.Name.Company: Letters, numbers, punctuation marks, spaces, and dots are allowed.

Behavior

  • Trailing spaces are automatically removed.
  • The HTML autocomplete attribute is automatically set:
    • name for Field.Name
    • given-name for Field.Name.First
    • family-name for Field.Name.Last
    • organization for Field.Name.Company

Validation rules

All name fields have the following validation rules:

  • Minimum length:
    • For Field.Name.First and Field.Name.Last: Names must be at least 1 character long.
    • For Field.Name.Company: Company names must be at least 3 characters long.
  • Pattern validation:
    • For Field.Name.First and Field.Name.Last: Names must start and end with a letter, and cannot contain consecutive hyphens or spaces. Only letters, spaces, and hyphens are allowed.
    • For Field.Name.Company: Must start and end with a letter or number, and cannot contain consecutive hyphens, spaces, or dots. Letters, numbers, punctuation marks, spaces, and dots are allowed in between.

The validation happens on blur, internally using the onBlurValidator property.

Note: The validation patterns are tailored to Norwegian name and company name requirements. If you need support for additional characters or different validation rules, you can extend the validation using the onBlurValidator property. See the Validators section below for more information.

Validators

Internal validators exposed

Field.Name and Field.Name.Company expose validators through their onBlurValidator property:

  • nameValidator: Validates names for Field.Name, Field.Name.First, and Field.Name.Last. It checks that the name:

    • Is at least 1 character long.
    • Matches the name pattern (starts and ends with a letter, no consecutive hyphens or spaces).
  • companyValidator: Validates company names for Field.Name.Company. It checks that the company name:

    • Is at least 3 characters long (default, can be customized via minLength prop).
    • Matches the company pattern (starts and ends with a letter or number, no consecutive hyphens, spaces, or dots).

You can extend the validation by providing your own onBlurValidator function. Access the internal validator through the validators parameter and combine it with your custom validation. This allows you to add additional validation rules while keeping the default validation intact.

import { Field } from '@dnb/eufemia/extensions/forms'
// Extend validation to require company names to contain "Corp"
const myValidator = (value, { validators }) => {
const { companyValidator } = validators
const customValidator = (value: string) => {
if (value && !value.includes('Corp')) {
return new Error('Company name must contain "Corp"')
}
}
return [companyValidator, customValidator]
}
render(<Field.Name.Company onBlurValidator={myValidator} />)