Help modifying SMF registration script (preg_match & preg_replace)

I am a noob with PHP and know enough to be dangerous.

I have already asked this question on the SMF support forum and the response have been general rather than specific. I hope that someone here will be able to help me.

The requirement I have is that when a user registers on the forum that their registration name must be their membership number which is a positive integer between 1 and 9999. I have identified the lines of code that i think need changing but all the changes I have made here do not do what I need.

[php]// 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 (stristr($context['checked_username'], $txt['guest_title']) !== false)
	$context['valid_username'] = false;

if (trim($context['checked_username']) == '')
	$context['valid_username'] = false;[/php]

Any help / code/ advice would be most welcome.


Colin

Adding this condition will be a to solution for your task:

[php] if (!preg_match(’~^([1-9][0-9]{0,3})$~’, $context[‘checked_username’]))
$context[‘valid_username’] = false;[/php]

In the above regular expression we require first symbol of username to be non-zero digit, followed by up to 3 digits 0-9.

Brilliant. :smiley:

Thanks very much that works perfectly.

Sponsor our Newsletter | Privacy Policy | Terms of Service