Thanks for your response. Initially I was trying to get fancy with only accepting a number between 1 and 9999, that is why I used the regex. Thinking about it again it doesn’t need to be that complicated as long as it is numeric then that will do.
I changed your code to be
[php]if(!is_numeric($context[‘checked_username’]))
$context[‘valid_username’] = false;[/php]
As I need it as an if statement rather than as a declaration within the context of SMF so I get the error messages if someone enters an invalid user name. Unfortunately my changes to your code (I am relatively inexperienced at PHP) mean that it still allows alpha characters as user name. Have I screwed up the logic of your statement or is this something else.
This is the entire code from the SMF function
[php]// See if a username already exists.
function RegisterCheckUsername()
{
global $sourcedir, $smcFunc, $context, $txt;
// This is XML!
loadTemplate('Xml');
$context['sub_template'] = 'check_username';
$context['checked_username'] = isset($_GET['username']) ? $_GET['username'] : '';
$context['valid_username'] = true;
// 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('/^\d+$/', 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;
if(!is_numeric($context['checked_username']))
$context['valid_username'] = false;
if (stristr($context['checked_username'], $txt['guest_title']) !== false)
$context['valid_username'] = false;
if (trim($context['checked_username']) == '')
$context['valid_username'] = false;
else
{
require_once($sourcedir . '/Subs-Members.php');
$context['valid_username'] &= isReservedName($context['checked_username'], 0, false, false) ? 0 : 1;
}
}[/php]
Any further help or advice will be most welcome.