Skip to content

Commit

Permalink
Merge pull request #6377 from OCHA-DAP/feature/HDX-9935-improve-usern…
Browse files Browse the repository at this point in the history
…ame-validation-logic

HDX-9935 improve live feedback validation for username
  • Loading branch information
danmihaila authored Jul 1, 2024
2 parents 0e69db0 + db1c7b6 commit 0196e38
Showing 1 changed file with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ this.ckan.module('hdx-form-validator', function ($) {
{rule: this.validateLength, args: [1, null]}
],
username: [
{rule: this.validateRegex, args: [/^[a-z0-9_-]+$/]},
{rule: this.validateLength, args: [2, 100]}
{rule: this.validateLowercaseAlphanumeric},
{rule: this.validateLength, args: [2, 100]},
{rule: this.validateCharacters, args: [/^[a-zA-Z0-9_-]+$/]},
],
email: [
{rule: this.validateRegex, args: [/^[^\s@]+@[^\s@]+\.[^\s@]+$/]}
Expand Down Expand Up @@ -166,12 +167,30 @@ this.ckan.module('hdx-form-validator', function ($) {
return [validationErrors.length === 0, validationErrors];
},


validateRegex: function (field, regex) {
var isValid = regex.test(field.val());
return [isValid, isValid ? null : 'invalid-format'];
},

validateLowercaseAlphanumeric: function (field) {
var value = field.val();

var isValid = true;
if (/[A-Z]/.test(value)) {
isValid = false;
}
if (!/[a-z0-9]/.test(value)) {
isValid = false;
}

return [isValid, isValid ? null : 'no-lowercase-alphanumeric'];
},

validateCharacters: function (field, regex) {
var isValid = regex.test(field.val());
return [isValid, isValid ? null : 'invalid-characters'];
},

validateFieldsMatch: function (field) {
var form = field.closest('form');
var matchFieldName = field.data('validation-match');
Expand Down Expand Up @@ -295,8 +314,8 @@ this.ckan.module('hdx-form-validator', function ($) {
var validationMessages = {
'name': [
{'key': 'invalid-length', 'message': 'Must be between 2 and 100 characters in length'},
{'key': 'invalid-format', 'message': 'Must use lowercase alphanumeric characters (a-z, 0-9)'},
{'key': null, 'message': 'Allowed special characters - (dash) or _ (underscore)'},
{'key': 'no-lowercase-alphanumeric', 'message': 'Must use lowercase alphanumeric characters (a-z, 0-9)'},
{'key': 'invalid-characters', 'message': 'Only allowed special characters - (dash) or _ (underscore)'},
],
'password1': [
{'key': 'invalid-length', 'message': 'The password must be a minimum of 10 characters in length'},
Expand Down

0 comments on commit 0196e38

Please sign in to comment.