Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Aiheen aloitti: Aaron10 - helmikuu 19, 2011, 01:36:48 IP

Otsikko: Set a username/display name character limit
Kirjoitti: Aaron10 - helmikuu 19, 2011, 01:36:48 IP
Wondering if its possible to limit usernames to say 12 characters long? Thanks.
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Aaron10 - helmikuu 20, 2011, 07:36:40 IP
bump
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Joker™ - helmikuu 21, 2011, 02:23:24 AP
Your SMF version?
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Aaron10 - helmikuu 21, 2011, 07:07:24 AP
1.1.13
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Joker™ - helmikuu 21, 2011, 09:01:54 AP
make backup of files before editing them

go to

Themes\Default\Register.template.php

Find:
<input type="text" name="user" size="20" tabindex="', $context['tabindex']++, '" maxlength="25" />


Replace it with:
<input type="text" name="user" size="10" tabindex="', $context['tabindex']++, '" maxlength="12" />


Find:
<input type="text" name="user" id="user_input" size="20" maxlength="12" />


Replace it with:
<input type="text" name="user" id="user_input" size="20" maxlength="12" />
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Aaron10 - helmikuu 21, 2011, 09:14:07 AP
Thanks a lot!
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Chris180 - helmikuu 12, 2014, 04:39:59 IP
Lainaus käyttäjältä: Joker™ - helmikuu 21, 2011, 09:01:54 AP
make backup of files before editing them

go to

Themes\Default\Register.template.php

Find:
<input type="text" name="user" size="20" tabindex="', $context['tabindex']++, '" maxlength="25" />


Replace it with:
<input type="text" name="user" size="10" tabindex="', $context['tabindex']++, '" maxlength="12" />


Find:
<input type="text" name="user" id="user_input" size="20" maxlength="12" />


Replace it with:
<input type="text" name="user" id="user_input" size="20" maxlength="12" />


Out of curiosity, why did you change the text size from 20 to 10?

Anyway, thanks very much :).
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Arantor - helmikuu 12, 2014, 04:48:08 IP
Because the size parameter doesn't affect content size but the size of the box shown to users. If it looks smaller they will be less inclined to write long names anyway.

Not that maxlength is even remotely guaranteeing anything; almost all modern browsers come with tools to edit the webpage, remove that limit and it'll be accepted. Needs to be enforced on the server, not the client.
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Ninja ZX-10RR - toukokuu 20, 2014, 01:29:52 AP
I'll bump this old topic up by asking how to do this in 2.0.7 because the file is obviously different but i have no idea of where to find such fields. Also i would like to set the limit for the name that is being shown (you can change it as an admin if you want but leaving the username unchanged).
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Arantor - toukokuu 20, 2014, 10:27:10 AP
Except the code is, as mentioned, not especially useful since a user can trivially bypass it.
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Ninja ZX-10RR - toukokuu 20, 2014, 11:03:06 AP
D: How could a user bypass it? Why? And, most importantly, how to fix it and anyway how to edit it?
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Arantor - toukokuu 20, 2014, 11:09:19 AP
Easy: press F12, inspect the element, remove the maxlength parameter. Chrome and Opera can do this and have been able to for ages. I think FF still needs Firebug to do this. Not sure about IE's dev tools lately, haven't used it.

Fixing it? Completely new code elsewhere in the bowels of SMF, not just changing the registration form.
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Ninja ZX-10RR - toukokuu 20, 2014, 11:17:08 AP
Thanks as always for the reply, quick as a lightning.

Well i don't think 99% of users would really be able to bypass it, i wouldn't think of it for instance even if i know what you are talking about.
I was hoping there was some sort of restriction somewhere in smf files! What the heck i think it is stupid leaving such an important feature like this buggy and not working perfectly. There must and i mean MUST be a code telling smf to accept names that are shorter than a given X value. If there is not such a string then it's vital to understand where the string is so at least FOR RIGHT NOW we can fix it and set a character limit, then for "smart" users using inspect element there will be a renaming action. But meantime where the heck did that string go? There is a limit of 25 right? So there must be a damn line telling smf "hei you must accept only usernames under 25 chars otherwise don't". Any clues?

*damn i feel always embarrassed and honoured just because you replied  :-[ my forum owes you so much, i will not forget it trust me*
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Arantor - toukokuu 20, 2014, 11:23:08 AP
It is working perfectly as designed. The fact you want a different design doesn't make it buggy. It merely makes it different.

You actually have to modify it in two places to make this work properly. First in Register.php:
if ($smcFunc['strlen']($context['checked_username']) > 25)
$context['checked_username'] = $smcFunc['htmltrim']($smcFunc['substr']($context['checked_username'], 0, 25));


Secondly in Subs-Members.php:
// Don't use too long a name.
if ($smcFunc['strlen']($regOptions['username']) > 25)
$reg_errors[] = array('lang', 'error_long_name');
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Ninja ZX-10RR - toukokuu 20, 2014, 11:35:11 AP
I don't actually want it different i just want it to be not-bypassable xD

Thanks anyway, as always flawless where you can.

MERGED POST
As you very quick i'll post again so i'm sure you will see. I'll merge posts when you reply.

Does this work for nicknames as well? I mean nicknames being different from usernames? I think you understood but i try to explain myself better anyway... As an admin i have the possibility to edit the name i can show and leaving the username with which i login untouched. I would like the shown names to use the same criteria. I mean not more than X value, should i have to edit something else or is it fine like this? And why? If you bother explaining me the reason as well xD

Thanks again, as always.
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Arantor - toukokuu 20, 2014, 11:42:51 AP
Display names are validated to be shorter than 60 characters. Changing *that* is even more complicated :P But it is possible to do.
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Ninja ZX-10RR - toukokuu 20, 2014, 11:48:05 AP
Crap xD i guess i'm gonna have to bother you again... Would you mind taking a look at it if you want and if you have time? I have no idea of where that could be as as far as i can see it's not close to the codes i have just edited following your instructions...

Thanks again, i guess i will have to create a canned thanks reply when you reply lol coz you always fix me something when you do.
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Arantor - toukokuu 20, 2014, 11:51:51 AP
It's primarily set by Profile-Modify.php:
'input_attr' => array('maxlength="60"'),
elseif ($smcFunc[\'strlen\']($value) > 60)
return \'name_too_long\';
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Ninja ZX-10RR - toukokuu 20, 2014, 01:11:09 IP
O.o only? It's even easier than the other one... Well thanks a lot, (again, *coughs*) NOW the topic is really SOLVED also for this SMF version. :D
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Arantor - toukokuu 20, 2014, 01:13:01 IP
For you, yes. For me, not so much since I had to figure out WTF it was set :P

Intercepting registration is easy, there are only two places it *can* be. But the code for the real name field, not so much. Technically there's an edge case I haven't covered but it's such an edge case that it doesn't matter (one can set their display name on registration even if the field isn't shown to users, along with a bunch of other stuff)
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Ninja ZX-10RR - toukokuu 20, 2014, 01:18:21 IP
Your SMF knowledge goes FAR beyond my comprehension limit, that's why you have almost 56k posts and i have 150... By the way i hope it will work i will test it after dinner and anyway no worries about the display name because like you said it's not shown in the registration page usually. Thanks again and if you have some other tips/adjustements/arantor tricks well let me know i will appreciate them all as always :D
Anyway i'll let you know whether it works or not :) but i think it will (just because YOU told me that xD)
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Arantor - toukokuu 20, 2014, 01:48:19 IP
Nah, it's just that I've spent so many hours studying the code that I know where to look for things and other than that, I just typed in '60' and searched, knowing that was the rule for it.

Quite sad, really, when you think about it, that I've spent so much time on it that I don't even need to think about some stuff any more.
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Ninja ZX-10RR - toukokuu 20, 2014, 02:26:02 IP
I know, i will definitely not say that but i'm judging only you as an SMF user. And you definitely know things better than anyone else here, outside of this you might be a murderer or a no-life nerd i don't care but from what i could see you are a legend ;)
P.s.: i don't really think all the bad things above huh! They are just examples ;)
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Ninja ZX-10RR - kesäkuu 12, 2014, 07:59:57 IP
A bit old topic but I will bump

My girlfriend made me think about a little issue... She always use strange usernames and she was able to register an account with an username of only 3 characters long. So now my question became: is it possible to set a minimum limit as well? :P
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Arantor - kesäkuu 12, 2014, 08:26:35 IP
In theory, sure, you'd just have to duplicate the code where it checks the upper limit to have it check the lower limit, though you'd need to add your own error messages because there aren't any in the SMF language files.
Otsikko: Re: Set a username/display name character limit
Kirjoitti: Ninja ZX-10RR - kesäkuu 12, 2014, 08:54:22 IP
Thanks :D I'll bump again when I'll try to do that, I don't want to get on codes now and as you know I'm not good at it so I want to think a few minutes about it ;)