Advertisement:

Author Topic: How can I limit username length?  (Read 2960 times)

Offline theserialcoder

  • Semi-Newbie
  • *
  • Posts: 17
How can I limit username length?
« on: March 06, 2012, 09:31:33 PM »
By default I believe usernames can be only 25 characters. How can I modify this to only allow, for example, usernames to be at most 15 characters?

Thanks for your help.

Offline K@

  • Lead Support Specialist
  • SMF Master
  • *
  • Posts: 46,204
  • Gender: Male
  • Yum!
Re: How can I limit username length?
« Reply #1 on: March 07, 2012, 07:24:51 AM »
You don't say which version of SMF this is for... :(

If it's v2.0.2, and this is a pure guess. So, keep a copy of the file, as a backup, in case I've bolloxed it up, OK?

In the default theme's directory, find Register.template.php

It looks, to me, like the " maxlength="25", in the first line, is what you're after. Dunno what the size=30 thing does, though.

Code: [Select]
<input type="text" name="user" id="smf_autov_username" size="30" tabindex="', $context['tabindex']++, '" maxlength="25" value="', isset($context['username']) ? $context['username'] : '', '" class="input_text" />
<span id="smf_autov_username_div" style="display: none;">
<a id="smf_autov_username_link" href="#">
<img id="smf_autov_username_img" src="', $settings['images_url'], '/icons/field_check.gif" alt="*" />
</a>
</span>
</dd>
<dt><strong><label for="smf_autov_reserve1">', $txt['email'], ':</label></strong></dt>
<dd>
<input type="text" name="email" id="smf_autov_reserve1" size="30" tabindex="', $context['tabindex']++, '" value="', isset($context['email']) ? $context['email'] : '', '" class="input_text" />
</dd>
<dt><strong><label for="allow_email">', $txt['allow_user_email'], ':</label></strong></dt>
<dd>
<input type="checkbox" name="allow_email" id="allow_email" tabindex="', $context['tabindex']++, '" class="input_check" />
</dd>
</dl>';

Offline theserialcoder

  • Semi-Newbie
  • *
  • Posts: 17
Re: How can I limit username length?
« Reply #2 on: March 07, 2012, 11:36:47 AM »
It is for v2.0.2, sorry about that. The size attribute for inputs just adjusts the width of the input field so the fact that it is 30 is fine. Maxlength would regulate the amount of input for the field on the client side but I'm more concerned with the server side validation. Someone can manipulate their client side code to allow them to input a longer name and I'd like to do a server side validation on the length of the username.

Offline K@

  • Lead Support Specialist
  • SMF Master
  • *
  • Posts: 46,204
  • Gender: Male
  • Yum!
Re: How can I limit username length?
« Reply #3 on: March 07, 2012, 01:01:53 PM »
To prove that this is way over my head, I have to confess that I have no idea what would control it, server-side.

I really thought that would've done the trick.

Let's hope that someone with a braincell sees this and can help you out. :)

Offline IchBin™

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 11,110
  • Gender: Male
  • I don't speak German.
    • IchBin.us
Re: How can I limit username length?
« Reply #4 on: March 07, 2012, 02:49:02 PM »
Brad "IchBin™" Grow        TinyPortal        Themes
Coding Guidelines       

Offline K@

  • Lead Support Specialist
  • SMF Master
  • *
  • Posts: 46,204
  • Gender: Male
  • Yum!
Re: How can I limit username length?
« Reply #5 on: March 07, 2012, 02:50:10 PM »
Good shout, Ichy-Wichy! :)

Offline Shambles

  • SMF Hero
  • ******
  • Posts: 2,796
  • Gender: Male
    • i30 Owners Club
Re: How can I limit username length?
« Reply #6 on: March 07, 2012, 02:51:02 PM »
Good shout, Ichy-Wichy! :)
Wish I could see it  :-\

Offline Illori

  • Doc Coordinator
  • SMF Master
  • *
  • Posts: 35,717
Re: How can I limit username length?
« Reply #7 on: March 07, 2012, 03:01:26 PM »
it is a thread in the charter board and for 1.1.1* as well.

that thread suggests looking in subs-members.php for the limit, it may have changed in 2.0 though.

Code: [Select]
// Don't use too long a name.
if ($smcFunc['strlen']($regOptions['username']) > 25)
$reg_errors[] = array('lang', 'error_long_name');

Offline IchBin™

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 11,110
  • Gender: Male
  • I don't speak German.
    • IchBin.us
Re: How can I limit username length?
« Reply #8 on: March 07, 2012, 03:13:50 PM »
Good shout, Ichy-Wichy! :)
Wish I could see it  :-\

Sorry about that. Search tricked me! ;)
Brad "IchBin™" Grow        TinyPortal        Themes
Coding Guidelines       

Offline theserialcoder

  • Semi-Newbie
  • *
  • Posts: 17
Re: How can I limit username length?
« Reply #9 on: March 10, 2012, 01:46:19 AM »
Thanks for your help.

I also found these lines in Register.php that should be changed.
Code: [Select]
if ($smcFunc['strlen']($context['checked_username']) > 25)
$context['checked_username'] = $smcFunc['htmltrim']($smcFunc['substr']($context['checked_username'], 0, 25));

Now that the server side seems to have been handled I don't know where to actually change the client side form's maxlength. Anyone know what file contains the html for the registration form so I can change this line?

Code: [Select]
<input type="text" name="user" id="smf_autov_username" size="30" tabindex="', $context['tabindex']++, '" maxlength="25" value="', isset($context['username']) ? $context['username'] : '', '" class="input_text" />

Offline Aleksi "Lex" Kilpinen

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 15,430
  • Gender: Male
  • The Artist Formerly Known as LexArma
Re: How can I limit username length?
« Reply #10 on: March 10, 2012, 02:00:59 AM »
I would think the layout is done in Themes/default/register.template.php
Finnish Support Local Moderator & Support Specialist
My Mods: Facebook and Twitter Sharer



Offline theserialcoder

  • Semi-Newbie
  • *
  • Posts: 17
Re: How can I limit username length?
« Reply #11 on: March 10, 2012, 02:55:48 AM »
I overlooked the filename in K@'s original post. Thanks again!

Offline Aleksi "Lex" Kilpinen

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 15,430
  • Gender: Male
  • The Artist Formerly Known as LexArma
Re: How can I limit username length?
« Reply #12 on: March 10, 2012, 02:58:01 AM »
No problem :) Are we ready to mark this solved, or do you still need help with this?
Finnish Support Local Moderator & Support Specialist
My Mods: Facebook and Twitter Sharer