General Community > Scripting Help

Custom Username Preg_match

(1/1)

kalium:
Hi,
could anyone help me with preg_match that should only alow: "^[A-Za-z0-9]+$", no spaces and  nospecial characters. I need clean Usernames, the user should take his desired name with all special chars and so on by using the Real Name option.

I dont get it to work, tested only some things like \s and it didn't care if i register with spaces in the name. Possible i didn't modifie the right files?

Thanks in advance! :)

/Sources/Subs-Members.php

--- Code: --- // Spaces and other odd characters are evil...
$regOptions['username'] = preg_replace('~[\t\n\r\x0B\0' . ($context['utf8'] ? ($context['server']['complex_preg_chars'] ? '\x{A0}' : "\xC2\xA0") : '\xA0') . ']+~' . ($context['utf8'] ? 'u' : ''), ' ', $regOptions['username']);

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

// Only these characters are permitted.
if (preg_match('~[<>&"\'=\\\\]~', preg_replace('~&#(?:\\d{1,7}|x[0-9a-fA-F]{1,6});~', '', $regOptions['username'])) != 0 || $regOptions['username'] == '_' || $regOptions['username'] == '|' || strpos($regOptions['username'], '[code') !== false || strpos($regOptions['username'], '[/code') !== false)
$reg_errors[] = array('lang', 'error_invalid_characters_username');
--- End code ---

./Subs/Register.php

--- Code: ---// Clean it up like mother would.
$context['checked_username'] = preg_replace('~[\t\n\r\x0B\0' . ($context['utf8'] ? ($context['server']['complex_preg_chars'] ? '\x{A0}' : "\xC2\xA0") : '\xA0') . ']+~' . ($context['utf8'] ? 'u' : ''), ' ', $context['checked_username']);
if ($smcFunc['strlen']($context['checked_username']) > 25)
$context['checked_username'] = $smcFunc['htmltrim']($smcFunc['substr']($context['checked_username'], 0, 25));

// Only these characters are permitted.
if (preg_match('~[<>&"\'=\\\]~', preg_replace('~&#(?:\\d{1,7}|x[0-9a-fA-F]{1,6});~', '', $context['checked_username'])) != 0 || $context['checked_username'] == '_' || $context['checked_username'] == '|' || strpos($context['checked_username'], '[code') !== false || strpos($context['checked_username'], '[/code') !== false)
$context['valid_username'] = false;
--- End code ---

emanuele:
Hello kalium and welcome to sm.org! :)

I think in Subs-Members.php you could try adding something like:

--- Code: --- if (!preg_match('~^[A-Za-z0-9]+$~', $regOptions['username']))
$reg_errors[] = array('lang', 'error_invalid_characters_username');

--- End code ---
just after the check for name length.
And similar in Register, something like:

--- Code: --- if (!preg_match('~^[A-Za-z0-9]+$~', $context['checked_username']))
$context['valid_username'] = false;
--- End code ---

kalium:
After short testing it seems to be working like a charme. It was too easy -.-

Hope someday anyone will help this answer too!

Thank you very much for your help and have a nice day! :)

Navigation

[0] Message Index

Go to full version