0

On my website, after a user registers they can change their username at any time. The minimum amount of characters is 6 and max amount is 25.

Here's some of the coding to check the length and remove characters:

$users_new_name = strip_tags($_POST['new_name']);
$new_name = preg_replace("/[^0-9a-zA-Z -]/", "", $users_new_name);
// Check Length
if (ctype_space($new_name)) {
$message = die('<span class="Fail">Error: Name Must Be At least 6 Characters Long!</i></span>');    
}
if(strlen($new_name) < 6) {
$message = die('<span class="Fail">Error: Name Must Be At least 6 Characters Long!</i></span>');
}
if(strlen($new_name) > 25) {
$message = die('<span class="Fail">Error: Name Can\'t Be More Than 25 Characters Long!</i></span>');
}

The issue I'm having is if you type in 5 spaces and then a letter or number, There new name will be that letter or number; Or if you type in a letter or number then 5 spaces. How could I prevent this from happening?

Here's a screenshot of the exampleenter image description here

3 Answers 3

1

I do not understand why tags would be in a POST.

Spaces becomes a non-issue if you change:

$users_new_name = strip_tags($_POST['new_name']);

To

$users_new_name = trim(strip_tags($_POST['new_name']));

Or ideally (strip tags is unnecessary):

$users_new_name = trim($_POST['new_name']);

Change the RegEx expression to /[^0-9a-zA-Z]/ to eliminate spaces and dashes.

Sign up to request clarification or add additional context in comments.

3 Comments

@user3059258 this only removes spaces from the front and back
@Misunderstood That's what I needed was from the front and the back, The trim Worked Perfectly!
The problem was also in the RegEx. It was saying spaces were allowed. [^ ] or [^x20] say to allow spaces. By removing the space dash. spaces and dashes would then be eliminated. /[^0-9a-zA-Z|x20|x2D]/ would have worked too.
1

It sounds like you need trim() to trim any spaces from the username. See http://php.net/trim

ltrim() will trim any leading spaces. rtrim() will trim any spaces at the end of the string.

5 Comments

That Worked Perfectly! But also, on the preg_replace Dashes aren't being removed, Not sure why!
@user3059258 In that case I would adjust the preg_replace, or do a simple str_replace on the username to remove dashes.
@user3059258 it'd because you added - at the end of the character block. For alphanumeric use "/[^0-9a-zA-Z ]/"
To simply allow alphanumeric, copy the regex in @kdogisthebest's comment.
I changed it to: $new_name = preg_replace("/[^0-9a-zA-Z ]/", "", $users_new_name); $new_name .= preg_replace('-', '', $users_new_name); Works Fine now
0

This is fairly simple and it's something I've had to deal with on systems before.

If you make the first thing you call, this preg_replace, the rest of you code should catch it;

$name = preg_replace('~[\s]+~', '', $name);

This simply checks if there is a space, or more than one, then replaces with a single space.

So " l" would return " l" - failing your 5 character minimum.

Use trim() around this and you should have it working to something acceptable (Depending on your definition of acceptable - worked in my use-case)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.