The <apex:inputSecret>
component is used to collect sensitive information, such as passwords, in a secure manner. It masks the input to ensure that the data is not visible to the user during input.
Why Use <apex:inputSecret>
?
- Securely collect sensitive data like passwords.
- Mask user input to protect privacy.
- Works similarly to the standard
<input>
field but with the added security of masking the characters.
Basic Syntax
xmlCopyEdit<apex:inputSecret value="{!passwordVariable}" label="Enter Password"/>
value
: Binds the input field to an Apex variable (typically a string).label
: Provides a label for the field, explaining what data is expected.
Example: Collecting a Password
xmlCopyEdit<apex:page controller="SecretInputController">
<apex:form>
<apex:inputSecret value="{!userPassword}" label="Enter Password"/>
<apex:commandButton value="Submit" action="{!submitForm}"/>
</apex:form>
</apex:page>
Apex Controller:
apexCopyEditpublic class SecretInputController {
public String userPassword { get; set; }
public void submitForm() {
System.debug('User Password: ' + userPassword);
}
}
Key Attributes
value
: Binds the field to an Apex variable for storing the entered password.label
: A descriptive label that guides users on what to enter.required
: Marks the field as mandatory for form submission.maxlength
: Limits the number of characters that can be entered.
Use Cases
- User Authentication: Collecting login credentials securely.
- Password Change Forms: Allow users to set or reset their passwords.
- Sensitive Data Collection: For any other instance where secure input is required.