Kryptix Posted December 30, 2009 Share Posted December 30, 2009 I need to validate form submission to check every character they have entered is a-z, A-Z, 0-9 and either a underscore or a space. How would I do this? Add every character to an array and then check or is there a better way? Quote Link to comment https://forums.phpfreaks.com/topic/186679-validating-characters/ Share on other sites More sharing options...
laffin Posted December 30, 2009 Share Posted December 30, 2009 $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); Quote Link to comment https://forums.phpfreaks.com/topic/186679-validating-characters/#findComment-985900 Share on other sites More sharing options...
Kryptix Posted December 30, 2009 Author Share Posted December 30, 2009 Thanks, could you explain it to me please? It's like reading Greek! Quote Link to comment https://forums.phpfreaks.com/topic/186679-validating-characters/#findComment-985977 Share on other sites More sharing options...
Daniel0 Posted December 30, 2009 Share Posted December 30, 2009 It matches case-insensitively strings that start with a letter a through z followed by 2 to 19 of either of the following things: word characters (as defined by the current locale), a space (ASCII 0x20) or an underscore, followed by end of string. Quote Link to comment https://forums.phpfreaks.com/topic/186679-validating-characters/#findComment-985985 Share on other sites More sharing options...
Psycho Posted December 30, 2009 Share Posted December 30, 2009 Here's a decent tutorial on regular expressions should you wish to learn more. It is one of the more abstract concepts in programming in my opinion. So be prepared to invest some time if you want to master it: http://www.regular-expressions.info/tutorial.html Quote Link to comment https://forums.phpfreaks.com/topic/186679-validating-characters/#findComment-985991 Share on other sites More sharing options...
Kryptix Posted December 31, 2009 Author Share Posted December 31, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/186679-validating-characters/#findComment-986233 Share on other sites More sharing options...
Kryptix Posted December 31, 2009 Author Share Posted December 31, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/186679-validating-characters/#findComment-986316 Share on other sites More sharing options...
cags Posted December 31, 2009 Share Posted December 31, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/186679-validating-characters/#findComment-986350 Share on other sites More sharing options...
.josh Posted December 31, 2009 Share Posted December 31, 2009 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] Quote Link to comment https://forums.phpfreaks.com/topic/186679-validating-characters/#findComment-986441 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.