Jump to content

Validating Characters


Kryptix

Recommended Posts

$valid=preg_match('/^[\w\x20_]+$/',$username);

this is prolly the easiest way, regex via preg_match.....

 

but I think I wud set up some more rules like like alpha 1st, and at a range of 3-20 characters, for some fields

$valid=preg_match('/^[a-z][\w\x20_]{2,19}$/i',$username);

 

 

 

 

Link to comment
Share on other sites

OK thanks a lot for your help. One last thing... How would I modify it so they can't use a space or a underscore at the start or the end of their name?

 

_INVALID_

_INVALID

INVALID_

VALID

VAL_ID

VaL_iD

Va_L_i_D

Invalid_

Inva!lid

Inv"&lid

 

Thank you very much for the time. I'm going to go and read up but if you could give me the exact regex needed it would help a LOT. :)

 

Also, what's the 2-19 about? I need to allow any number, anywhere, as well as it's 0-9. Can you also define how long a name can be using this? If so, the max needs to be 11.

Link to comment
Share on other sites

Actually, ignore that, I no longer want to allow underscores. I'm just going to only allow spaces, letters and numbers now so I can simply trim() the entry thus getting rid of the spaces at the start and end.

 

if (!preg_match('/^[\w\x20]+$/', trim($_GET['x']))) {

echo "Invalid";

} else if (strlen(trim($_GET['x'])) < 3) {

echo "Too short";

} else if (strlen(trim($_GET['x'])) > 11) {

echo "Too long";

} else {

echo "Valid";

}

 

Can someone please give me the correct regex that I need? I simply removed the underscore in this one but it's still allowing underscores. Thanks a lot for your help.

Link to comment
Share on other sites

You should be careful about using trim in that manner without informing the user, if they believe it to be a valid character they may purposefully put it into their 'username'. By stripping it without telling them you have changed their username on them. Since we're talking about whitespace at the beginning and end it's probably no the end of the world. On T-Mobiles site they stripped all underscores from my username without telling me, it took me an age and a day to work out why I couldn't log in.

Link to comment
Share on other sites

to remove the underscore, change \w to a-zA-Z0-9

 

[ot]

dude the new t-mobile site redesign sucks monkey balls. It has a ton of bugs in it that make me scared of seeing my bill each month.  Twice now I've had to call up customer service (which is also shitty, btw), to correct some billing inconsistency due to bugs on their site. I'm glad my contract with them is over in Feb

[/ot]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.