hook()
Run JavaScript logic for input validation criteria.
Setup
Attribute: sf-validation="hook(HOOK_NAME)"
Where: Apply to any input
element in your form.
Utilize custom JavaScript functions identified by HOOK_NAME
to conduct unique validation checks on form inputs.
Initialization
Add the
sf-validation="hook(HOOK_NAME)"
attribute to your forminput
element, such as thetext
input.Then, register the hook using the Superform API. See the example below.
Example in steps:
Adding
hook(validateEmail)
to the HTMLinput
element.This calls a function
validateEmail
registered with JavaScript to check if the input is a valid email.If not, Superform triggers the corresponding error.
HTML Example
<!-- Form Container -->
<div sf="myForm">
<form>
<!-- Email input with validation hook and error message -->
<label for="email">Email:</label>
<input type="email" id="email" name="email" sf-validation="hook(validateEmail)" placeholder="Enter your email">
<!-- Error message that appears if the email includes '@gmail.com'-->
<div class="text-style-error">
'@gmail.com' is not allowed. Please use a different email.
</div>
<button sf-goto="next">Next</button>
</form>
</div>
// Wait for Superform to be ready before registering hooks
window.SuperformAPI = window.SuperformAPI || [];
window.SuperformAPI.push(({ getForm }) => {
// Fetch the specific form by its name
const myForm = getForm("myForm");
// Register a validation hook for the email input
myForm.registerInputValidationHook("validateEmail", ({ data }) => {
// Access the 'email' field from the form data
const emailInput = data.email;
// Check if the email address includes '@gmail.com'
if (emailInput.includes("@gmail.com")) {
// If it includes '@gmail.com', return false (invalid - triggers error)
return false;
} else {
// If it does not include '@gmail.com', return true (valid)
return true;
}
});
});
Webflow Example
Attribute Configuration
Ensure the attribute key is
sf-validation
and the value ishook(HOOK_NAME)
Ensure the hook name is unique and does not contain spaces or special characters.
Do not register multiple hooks with the same name. The default nature of hook registration is to override.
Hooks must return a
boolean
value indicating the validity of the input.
→ Read more about the registerInputValidationHook
method here.
Last updated
Was this helpful?