Hi guys,
is there a snippet to
- limit characters for the username and
- forbid @ or . (etc.)?
otherwise it can look like this:
Hi guys,
is there a snippet to
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.
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
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
);
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
);