Skip to content

Commit

Permalink
Added 'equalTo' Validator
Browse files Browse the repository at this point in the history
  • Loading branch information
jonacs committed Nov 27, 2023
1 parent 20eb614 commit 45b8e7a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ if (validationResults.some(result => !result)) {
- **`numeric`**: Validates if the input is a numeric value.
- **`numeric-range`**: Validates if the numeric input is within a specified range.
- **`date`**: Validates if the input is a valid date.
- **`strongPassword`**: Validates if the input is a strong password.Contributing
- **`strongPassword`**: Validates if the input is a strong password.
- **`equalTo`**: Validates if the input is equal to value provided.

## Contributing

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "acs-bootstrap-input-validator",
"version": "1.0.8",
"version": "1.0.9",
"description": "A javascript input validator for bootstrap",
"keywords": [
"input",
Expand Down
18 changes: 17 additions & 1 deletion src/inputValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Bootstrap Input Validator
* A versatile input validation library for web applications, designed to work seamlessly with Bootstrap.
*
* @version 1.0.8
* @version 1.0.9
* @copyright 2023 Jonathan Hayman
* @license MIT
*/
Expand Down Expand Up @@ -73,6 +73,11 @@ export default class InputValidator {
errorMessages.push((validationMessages?.strongPassword || 'Not strong enough. Minimum 8 characters, at least one uppercase letter, one lowercase letter, and one number.'));
}

// Check if 'equalTo' validation is present and value matches the specified value
if (validationParams.equalTo && !this.isEqualTo(element.value, validationParams.equalTo)) {
errorMessages.push((validationMessages?.equalTo || 'Input value does not match.'));
}

// Check if 'customRegex' validation is present and value doesn't match the custom regex pattern
if (validationParams.customRegex && !this.customRegexValidation(element.value, validationParams.customRegex)) {
errorMessages.push((validationMessages?.customRegex || 'Custom validation failed.'));
Expand Down Expand Up @@ -263,4 +268,15 @@ export default class InputValidator {
const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/;
return passwordRegex.test(password);
}

/**
* Validates if a given value is equal to the value of the input element
*
* @param {string} inputValue - The input value to be validated.
* @param {string} equalValue - The value to be compared to.
* @return {boolean} - True if the input value is equal to the equal value, otherwise false.
*/
static isEqualTo(inputValue, equalValue) {
return inputValue === equalValue;
}
}
35 changes: 35 additions & 0 deletions tests/isEqualTo.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import InputValidator from '../src/inputValidator';

test('Valid Equal to', () => {
expect(InputValidator.isEqualTo('10', '10')).toBe(true);
});

test('Invalid Equal to', () => {
expect(InputValidator.isEqualTo('10', '20')).toBe(false);
});

test('Check if equalTo custom error message is applied', () => {
// Create a container div to hold the input and potential error div
const containerDiv = document.createElement('div');

// Create an input element
const inputElement = document.createElement('input');
inputElement.value = '10';

// Append the input to the container
containerDiv.appendChild(inputElement);

// Validation parameters and messages
const validationParams = { "equalTo": '20' };
const validationMessages = { "equalTo": 'Inputs do not match!' };

// Assuming your InputValidator inserts an error message as a sibling
InputValidator.validateInput(inputElement, validationParams, validationMessages);

// Check if the error message is inserted as a sibling to the input
const errorMessageDiv = inputElement.nextElementSibling;

expect(errorMessageDiv).not.toBeNull();
expect(errorMessageDiv.classList.contains('invalid-feedback')).toBe(true);
expect(errorMessageDiv.innerHTML).toBe('Inputs do not match!');
});

0 comments on commit 45b8e7a

Please sign in to comment.