SF-PD2 User Interface Practice Question
A developer is creating a Lightning Web Component that includes a lightning-input field for a phone number. The component must validate that the input matches a specific pattern before allowing the user to submit a form. The developer wants to use the built-in validation features of lightning-input. Which attribute should be set to enforce a custom validation pattern?
⚠ Common exam trap
The trap here is assuming that a custom attribute like regex or validation exists for pattern matching, when the component actually follows the standard HTML pattern attribute.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
pattern
The pattern attribute is the correct choice because it allows the developer to specify a regular expression that the input value must match. When the form is submitted, the browser's constraint validation API checks the input against this pattern and prevents submission if it fails. The other attribute names do not exist on lightning-input. Using pattern is the standard, declarative way to add custom validation to a lightning-input field without writing additional JavaScript.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
validation
Why it's wrong here
There is no attribute named validation on lightning-input. The component provides attributes like required, min-length, max-length, and pattern for basic validation, but validation is not a valid attribute. Attempting to use it will have no effect and may cause a warning in the console. Developers must use the documented attributes for validation purposes. Custom validation logic beyond the built-in attributes requires JavaScript using setCustomValidity or custom error handling.
- ✗
regex
Why it's wrong here
The regex attribute does not exist on lightning-input. While the concept of using a regular expression for validation is correct, the attribute name is pattern, not regex. Using an unrecognized attribute will not trigger any validation behavior. Developers familiar with other frameworks might assume a regex attribute, but Salesforce's Lightning Web Components follow the HTML standard for input validation, which uses pattern. Always refer to the official component documentation for supported attributes.
- ✗
validity
Why it's wrong here
The validity attribute is not a supported attribute on lightning-input. The lightning-input component does expose a validity property in JavaScript that returns a ValidityState object, but it is read-only and not used to set validation rules. To enforce a pattern, the pattern attribute must be used. The validity property is useful for checking the current validation state programmatically, but it does not define the validation criteria. Setting an attribute named validity will not work.
- ✓
pattern
Why this is correct
The pattern attribute on lightning-input accepts a regular expression string that the input value must match. When the form is submitted, the browser's native constraint validation API checks the value against the pattern. If it does not match, the input is marked invalid and the form submission is blocked. This provides a simple way to enforce custom validation without writing extra JavaScript. The pattern attribute works with the standard HTML pattern attribute semantics.
About these practice questions
One of 226 original SF-PD2 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official Salesforce exam blueprint
This SF-PD2 practice question is part of Courseiva's free Salesforce certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the SF-PD2 exam.