Password input

Create password inputs in your forms.

Password input

Use the passwordInput() function to create password inputs in your forms. It uses the HTML <input type="password"> element which masks the input value for security.


Basic usage

import { Composer } from "formsmd";

const composer = new Composer({
  id: "my-form"
});

composer.passwordInput("password", {
  question: "Create a password"
});

Generates the following Markdown-like syntax:

#! id = my-form

password = PasswordInput(
  | question = Create a password
)

Required

Add the required parameter to make the field mandatory:

composer.passwordInput("password", {
  question: "Create a password",
  required: true
});

Generates the following Markdown-like syntax:

password* = PasswordInput(
  | question = Create a password
)

Function overview

The following is the overview of the function:

passwordInput(name: string, params: object)

Arguments

Name
Type
Description

name

string

A unique name for your form field that you'll use to identify the user's response.*

params

object

An object containing all the configuration parameters for your password input field (see the parameters section below for the full list of options).


Parameters

Shared parameters

These parameters are common to all form fields:

Name
Type
Description

question (required)

string

The main question or label of the form field.

required

true (boolean)

When set, the field becomes required.

description

string

Any extra information that the user may need to fill out the form. Appears right below the question.

fieldSize

"sm"

When set to "sm", the font sizes of the question, description, and answer are made smaller.

labelStyle

"classic"

When set to "classic", the question and description of the form field are made smaller.

subfield

true (boolean)

When set, the question and description of the form field are made smaller. Functionally the same as setting labelStyle to "classic".

disabled

true (boolean)

When set, the input is disabled.

autofocus

true (boolean)

When set, the input will be automatically focused when the parent slide becomes active, or immediately after page load.

id

string

The id attribute of the form field container.

classNames

string[]

The CSS class names of the form field. See the available CSS utility classes.

attrs

Array<{ name: string, value: string }>

Other HTML attributes of the form field. Each attribute has a name and value property.

displayCondition

{ dependencies: string[], condition: string }

Controls when the field is shown. The dependencies lists the fields to watch, and condition is the expression that must be true to show the field. The condition must be a valid Nunjucks expression. See example.

Password input specific parameters

Name
Type
Description

placeholder

string

Sets the placeholder attribute of the input.

maxlength

number

If set, this becomes the maximum number of allowed characters in the input.

pattern

string

If set, the input value must match the given pattern.

value

string

If set, this becomes the default value of the input.


Examples

Password input with custom placeholder

composer.passwordInput("newPassword", {
  question: "Create a new password",
  description: "Must be at least 8 characters long",
  placeholder: "Enter your password",
  required: true
});

Generates the following Markdown-like syntax:

newPassword* = PasswordInput(
  | question = Create a new password
  | description = Must be at least 8 characters long
  | placeholder = Enter your password
)

Password input with pattern validation

composer.passwordInput("password", {
  question: "Create a password",
  description: "Must contain at least one uppercase letter, one lowercase letter, and one number",
  pattern: "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).{8,}$",
  required: true
});

Generates the following Markdown-like syntax:

password* = PasswordInput(
  | question = Create a password
  | description = Must contain at least one uppercase letter, one lowercase letter, and one number
  | pattern = ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$
)

Styled password input with custom attributes

Add CSS classes and other HTML attributes using the classNames and attrs parameters. Please note, these class names and attributes are added to the <div> or <fieldset> container that contains the actual input field(s).

composer.passwordInput("accountPassword", {
  question: "Account password",
  classNames: ["col-6", "xs:col-6"],
  attrs: [
    { name: "style", value: "font-size: 18px;" }
  ]
});

Generates the following Markdown-like syntax:

[.col-6 .xs:col-6 style="font-size: 18px;"]
accountPassword = PasswordInput(
  | question = Account password
)

Please see the available CSS utility classes.

Conditional display

Conditionally show or hide a password input field using the displayCondition parameter. It works as follows:

  • dependencies lists the fields to watch.

  • condition is the expression that must be true to show the field. This must be a valid Nunjucks expression.

For instance, in the example below, the password input will only show up if the user chooses to create an account.

composer.choiceInput("createAccount", {
  question: "Would you like to create an account?",
  required: true,
  choices: ["Yes", "No"]
})

composer.passwordInput("accountPassword", {
  question: "Create a password",
  description: "Must be at least 8 characters long",
  displayCondition: {
    dependencies: ["createAccount"],
    condition: "createAccount == 'Yes'"
  }
});

Generates the following Markdown-like syntax:

createAccount* = ChoiceInput(
  | question = Would you like to create an account?
  | choices = Yes, No
)

::: [{$ createAccount $}]
{% if createAccount == "Yes" %}
accountPassword = PasswordInput(
  | question = Create a password
  | description = Must be at least 8 characters long
)
{% endif %}
:::

Last updated

Was this helpful?