diff --git a/README.md b/README.md index 7bf9bc5..2018ee0 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index 5e53c83..d5ceb41 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/inputValidator.js b/src/inputValidator.js index e9d5b2a..774d2ee 100644 --- a/src/inputValidator.js +++ b/src/inputValidator.js @@ -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 */ @@ -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.')); @@ -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; + } } diff --git a/tests/isEqualTo.test.js b/tests/isEqualTo.test.js new file mode 100644 index 0000000..169158b --- /dev/null +++ b/tests/isEqualTo.test.js @@ -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!'); +}); \ No newline at end of file