Username length and characters

Hi guys,

is there a snippet to

  1. limit characters for the username and
  2. forbid @ or . (etc.)?

otherwise it can look like this:

Hi,
You can do it with CSS

example:

.name of your class (change this with the class that contain username) {
  width:95%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

*text-overflow: ellipsis; add 3 dots at the end of username
Please try and see if its right for you.

2 Likes

Thanks buddy. That would be a solution for the design.

I’d need a snippet for not allowing mail as username though, since a lot of users use auto-fill and some of it will fill out the username-field with the email address.

Hi,

Please try to use these PHP snippets:

For forbid @ and other symbols (only letters, numbers, dash, and underscores will be available):

add_filter( 
 'hivepress/v1/models/user', 
 function( $model  ) { 
  if ( isset($model ['fields']['username'] ) ) { 
   $model['fields']['username']['pattern'] = '([a-zA-Z0-9_-]+)';
   $model['fields']['username']['max_length'] = 10; 
  } 
 
  return $model ; 
 }, 
 1000 
); 

Limit characters to 10:

add_filter(
	'hivepress/v1/forms/user_register',
	function( $form ) {
		if ( isset( $form['fields']['username'] ) ) {
		
			$form['fields']['username']['max_length'] = 10;
		}

		return $form;
	},
	1000
);

Please note that it can require further customization.

​I hope this is helpful to you.

Thanks for the code it works great! is it possible that it should say characters or not allowed cause now the user doesn’t understand what’s wrong
image

Is it possible for you to sign up at all @Abe ?

The code didn’t work for me and users couldn’t sign up even using only letters. I’ll dig deeper into this tomorrow.

Yes you’re right users cant sign up at all with this code

Hi,

Sorry for the confusion.

Please try this PHP snippet:

add_filter( 
 'hivepress/v1/models/user', 
 function( $model  ) { 
  if ( isset($model ['fields']['username'] ) ) { 
   $model['fields']['username']['pattern'] = '([a-zA-Z0-9_-]+)';
   $model['fields']['username']['max_length'] = 10; 
  } 
 
  return $model ; 
 }, 
 1000 
);
1 Like


As you can see on the screenshot, when there is more than ten characters it says that the problem is that there’s more than ten characters. But when you add symbols, it doesn’t say what the problem is, it just says “Please match the requested format”. Can this be changed? because the user won’t understand what’s wrong.

You can try this code snippet instead, to add a hint to the username field:

add_filter( 
 'hivepress/v1/models/user', 
 function( $model  ) { 
  if ( isset($model ['fields']['username'] ) ) { 
   $model['fields']['username']['pattern'] = '([a-zA-Z0-9_-]+)';
   $model['fields']['username']['max_length'] = 10;
   $model['fields']['username']['description'] = 'please use alphanumeric characters and numbers only'; 
  }
 
  return $model ; 
 }, 
 1000 
);
2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.