User Input Validation
H:Dev+ provides input validation functionality to handle user data safely and consistently within input forms.
You can define validation attributes on input fields and run checks either in real time or on submission, according to the validity conditions.
Input Validation Flow
You can configure input validation by following these steps:
- Identify the validation pattern for the data to be validated
- Set the validation pattern using the data-validation attribute
- Set whether real-time replacement applies using the data-validation-type attribute (optional)
- For checkboxes, set the selection count limit using data-validation-limit
Validation Patterns
| Pattern Name | Value |
|---|---|
| phone-auto-hyphen | Phone number format including '-' hyphensReplace supported |
| phone | Numeric phone number formatReplace supported |
| phone-compact | Phone number in 0000-0000 format, excluding the leading digits such as the area codeReplace supported |
| EmailReplace supported | |
| domain | Domain including http or https |
| password-8-32 | 8-32 character format with digits, letters, and special characters (?=.*[~!@#$%^*()+-=[]{};:?.,_)Replace supported |
| password-4-8 | 4-8 character format with digits, letters, and special characters (?=.*[~!@#$%^*()+-=[]{};:?.,_)Replace supported |
| number | DigitsReplace supported |
| number-alphabet | Digits + lettersReplace supported |
| number-alphabet-underscore | Digits + letters + underscore ('_')Replace supported |
| not-empty | Input required |
| ip-v4 | IP address in IPv4 format (000.000.000.000)Replace supported |
| check-limit | Validates the minimum and maximum number of checkbox selections set via the 'data-validation-limit' attribute |
| radio-check | Validates whether a radio button is selected |
HTML attributes
Tag : <form>
| Name | Value | Description |
|---|---|---|
| data-validation-alert | yes or undefined | Display an alert when input validation fails |
Tag : <input>
| Name | Value | Description |
|---|---|---|
| data-validation | Validation pattern | Refer to the Validation Patterns section above |
| data-validation-type | replace or undefined | For patterns that support replacement, replaces the input value into the corresponding pattern format |
| data-invalid-message | String | Message to display when input validation fails |
Tag : <div> (Checkbox group - applied to the parent element)
| Name | Value | Description |
|---|---|---|
| data-validation | check-limit | Selection count validation pattern |
| data-validation-limit | 1-3 | Minimum and maximum selectable count in 'min-max' format |
Tag : div(parent tag of radio buttons)
| Name | Value | Description |
|---|---|---|
| data-validation | radio-check | Radio button selection required |
HTML
<form id="frm_validation">
<!-- phone-auto-hyphen : phone number format with '-' hyphens included -->
<div class="mb-3">
<label class="form-label">phone-auto-hyphen</label>
<input type="text" class="form-control" data-validation="phone-auto-hyphen" data-validation-type="replace" placeholder="Phone number format with '-' hyphens"/>
<small class="invalid-feedback">Please enter a valid value</small>
<small class="valid-feedback">OK</small>
</div>
<!-- phone-compact : phone number in 0000-0000 format excluding the area code and other prefixes -->
<div class="mb-3">
<label class="form-label">phone-compact</label>
<input type="text" class="form-control" data-validation="phone-compact" data-validation-type="replace" placeholder="Numeric phone number format"/>
<small class="invalid-feedback">Please enter a valid value</small>
<small class="valid-feedback">OK</small>
</div>
<!-- email : email -->
<div class="mb-3">
<label class="form-label">email</label>
<input type="text" class="form-control" data-validation="email" data-validation-type="replace" placeholder="Email"/>
<small class="invalid-feedback">Please enter a valid value</small>
<small class="valid-feedback">OK</small>
</div>
<!-- check-limit : validates the minimum and maximum number of checkbox selections set via the 'data-validation-limit' attribute -->
<div class="mb-3">
<label class="form-label">check-limit</label>
<div class="form-control d-flex justify-content-start align-items-center" data-validation="check-limit" data-validation-limit="2-4">
<div class="form-check-inline d-flex justify-content-start align-items-center">
<input class="form-check-input" type="checkbox" value="" id="ck1">
<label class="form-check-label small" for="ck1">
Check 1
</label>
</div>
<div class="form-check-inline d-flex justify-content-start align-items-center">
<input class="form-check-input" type="checkbox" value="" id="ck2">
<label class="form-check-label small" for="ck2">
Check 2
</label>
</div>
<div class="form-check-inline d-flex justify-content-start align-items-center">
<input class="form-check-input" type="checkbox" value="" id="ck3">
<label class="form-check-label small" for="ck3">
Check 3
</label>
</div>
<div class="form-check-inline d-flex justify-content-start align-items-center">
<input class="form-check-input" type="checkbox" value="" id="ck4">
<label class="form-check-label small" for="ck4">
Check 4
</label>
</div>
<div class="form-check-inline d-flex justify-content-start align-items-center">
<input class="form-check-input" type="checkbox" value="" id="ck5">
<label class="form-check-label small" for="ck5">
Check 5
</label>
</div>
</div>
<small class="invalid-feedback">You can select 2 to 4 items.</small>
<small class="valid-feedback">OK</small>
</div>
</form>