Last active
August 29, 2015 14:19
-
-
Save nairdo/3431820b3bf9c76561ad to your computer and use it in GitHub Desktop.
A way to allow admin changeable validation for usernames created in the AccountEntry block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var usernameRegExp = new RegExp("<%= GetAttributeValue( "ValidUsernameRegularExpression" ) %>"); | |
var usernameValidDescription = "<%= GetAttributeValue( "ValidUsernameDescription" ) %>"; | |
availabilityMessageRow.hide(); | |
usernameTextbox.blur(function () { | |
if ($(this).val() && $.trim($(this).val()) != '') { | |
if (! usernameRegExp.test($(this).val())) { | |
usernameUnavailable.html('Username is not valid! ' + usernameValidDescription ); | |
usernameUnavailable.addClass('alert-warning'); | |
usernameUnavailable.removeClass('alert-success'); | |
} | |
else { | |
$.ajax({ | |
type: 'GET', | |
contentType: 'application/json', | |
dataType: 'json', | |
url: Rock.settings.get('baseUrl') + 'api/userlogins/available/' + escape($(this).val()), | |
success: function (getData, status, xhr) { | |
if (getData) { | |
usernameUnavailable.html('That username is available.'); | |
usernameUnavailable.addClass('alert-success'); | |
usernameUnavailable.removeClass('alert-warning'); | |
} else { | |
availabilityMessageRow.show(); | |
usernameUnavailable.html('That username is already taken!'); | |
usernameUnavailable.addClass('alert-warning'); | |
usernameUnavailable.removeClass('alert-success'); | |
} | |
}, | |
error: function (xhr, status, error) { | |
alert(status + ' [' + error + ']: ' + xhr.responseText); | |
} | |
}); | |
} | |
} else { | |
usernameUnavailable.html('Username is required!'); | |
usernameUnavailable.addClass('alert-warning'); | |
usernameUnavailable.removeClass('alert-success'); | |
} | |
availabilityMessageRow.show(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// two new block properties to control the valid regular expression and the human readable version | |
[TextField( "Valid Username Regular Expression", "A regular expression that is used to validate good, valid usernames.", false, "^[A-Za-z0-9+.@_-]{3,128}$" )] | |
[TextField( "Valid Username Description", "Human description of a valid username.", false, "It must only contain letters, numbers, +, -, _, or @. It must also be longer than three characters and less than 128." )] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment