Skip to content

Instantly share code, notes, and snippets.

@richkevan
Last active February 4, 2024 01:02
Show Gist options
  • Save richkevan/e1732195991bcd9d9d42dfd59c82a0d3 to your computer and use it in GitHub Desktop.
Save richkevan/e1732195991bcd9d9d42dfd59c82a0d3 to your computer and use it in GitHub Desktop.
Username Validation using single regex string to test for all conditions

Codeland Username Validation Have the function CodelandUsernameValidation(str) take the str parameter being passed and determine if the string is a valid username according to the following rules:

  1. The username is between 4 and 25 characters.
  2. It must start with a letter.
  3. It can only contain letters, numbers, and the underscore character.
  4. It cannot end with an underscore character.

If the username is valid then your program should return the string true, otherwise return the string false.

Examples

Input: "aa_"
Output: false
Input: "u__hello_world123"
Output: true
// Codeland Username Validation
// Have the function CodelandUsernameValidation(str) take the str parameter being passed and determine if the string is a valid username according to the following rules:
// 1. The username is between 4 and 25 characters.
// 2. It must start with a letter.
// 3. It can only contain letters, numbers, and the underscore character.
// 4. It cannot end with an underscore character.
// If the username is valid then your program should return the string true, otherwise return the string false.
// UNIFIED REGEX SOLUTION
function CodelandUsernameValidation(str) {
const unifiedRegex = /^[A-Za-z][\w\d]{2,23}[A-Za-z0-9]$/
return (unifiedRegex.test(str))
}
// keep this function call here
module.exports = CodelandUsernameValidation;
const CodelandUsernameValidation = require('../index.js');
test('CodelandUsernameValidation, u__hello', () => {
expect(CodelandUsernameValidation('u__hello')).toBe(true);
});
test('CodelandUsernameValidation, User1234', () => {
expect(CodelandUsernameValidation('User1234')).toBe(true);
});
test('CodelandUsernameValidation, 1234User', () => {
expect(CodelandUsernameValidation('1234User')).toBe(false);
});
test('CodelandUsernameValidation, AlphaBeta22', () => {
expect(CodelandUsernameValidation('AlphaBeta22')).toBe(true);
});
test('CodelandUsernameValidation, testUser_1', () => {
expect(CodelandUsernameValidation('testUser_1')).toBe(true);
});
test('CodelandUsernameValidation, User_', () => {
expect(CodelandUsernameValidation('User_')).toBe(false);
});
test('CodelandUsernameValidation, dev2024', () => {
expect(CodelandUsernameValidation('dev2024')).toBe(true);
});
test('CodelandUsernameValidation, a', () => {
expect(CodelandUsernameValidation('a')).toBe(false);
});
test('CodelandUsernameValidation, LongUsername_1234567890123456', () => {
expect(CodelandUsernameValidation('LongUsername_1234567890123456')).toBe(false);
});
test('CodelandUsernameValidation, codeMaster', () => {
expect(CodelandUsernameValidation('codeMaster')).toBe(true);
});
test('CodelandUsernameValidation, Special!User', () => {
expect(CodelandUsernameValidation('Special!User')).toBe(false);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment