Nuv Posted February 4, 2011 Share Posted February 4, 2011 Hi guys, Ive been coding a script and really stuck with preg.Hope you guys will help me out.Below is my problem :- I want people to enter their usernames in a field while registration but it should only accept the range from a-z,A-Z and `1 `2 `3 `4 `5 `6 `7 `8 `9 `! `@ `# `$ `% `^ `& `* `( `) Please Note Suppose i input `% then there should be no gap between ` and % . Likewise, If i enter `4 , there should be no gap between ` and 4. I dont want my users to enter numbers but if that number is with ` like `4 or `6....it should be allowed. Also no space in between usernames should be allowed. Quote Link to comment https://forums.phpfreaks.com/topic/226677-problem-with-preg_match-have-no-clue-how-to-achieve-it/ Share on other sites More sharing options...
Nuv Posted February 4, 2011 Author Share Posted February 4, 2011 I have come up with a regular expression ^(([a-zA-Z])|`([1-9!@#$%^&*()])) Is it alright ? Quote Link to comment https://forums.phpfreaks.com/topic/226677-problem-with-preg_match-have-no-clue-how-to-achieve-it/#findComment-1169833 Share on other sites More sharing options...
Nuv Posted February 4, 2011 Author Share Posted February 4, 2011 Above RE is wrong..this one is better, except is it accepting space which i dont want /^[a-zA-Z]|`[1-9!@#$%^&*()]/ Quote Link to comment https://forums.phpfreaks.com/topic/226677-problem-with-preg_match-have-no-clue-how-to-achieve-it/#findComment-1169840 Share on other sites More sharing options...
Nuv Posted February 4, 2011 Author Share Posted February 4, 2011 Above regular expression is also wrong..this is the best i have '/^`[1-9!@#$%^&*()]{1}|[a-zA-z]/' Quote Link to comment https://forums.phpfreaks.com/topic/226677-problem-with-preg_match-have-no-clue-how-to-achieve-it/#findComment-1169872 Share on other sites More sharing options...
.josh Posted February 4, 2011 Share Posted February 4, 2011 okay so let me get this straight: 1) The string should always start with ` 2) After the ` the string can contain 1 or more (is there a limit?) of any letter, number or the following symbols: !@#$%^&*() 3) No spaces anywhere (which this is technically defined by its absence in #2) Quote Link to comment https://forums.phpfreaks.com/topic/226677-problem-with-preg_match-have-no-clue-how-to-achieve-it/#findComment-1169878 Share on other sites More sharing options...
Nuv Posted February 4, 2011 Author Share Posted February 4, 2011 okay so let me get this straight: 1) The string should always start with ` 2) After the ` the string can contain 1 or more (is there a limit?) of any letter, number or the following symbols: !@#$%^&*() 3) No spaces anywhere (which this is technically defined by its absence in #2) 1,2.Not the string, numbers from 1-9 and !@#$%^&*() should start with ` . That to only one number(or symbol) not many.Like - `$ <--- Right `$% <--- Wrong (As it has more than 1 number[or symbol]) 3.Yes, i dont want spaces anywhere in the string. However, Range from a-zA-z should NOT start from ` Few examples to understand better.. craY`5on <-- Right (as a-zA-Z range has no ` in front and number 5 has `) cr`$ayon<--Right(as a-zA-Z range has no ` in front and symbol $ has `) cr`aYon<--Wrong(a which comes under range a-zA-Z has `) cr`$%ayon<--Wrong(2 symbols have one `.% should have had ` too for it to be correct) cr`5$ayon<--Wrong(1 symbol and 1 number has one `.$ should have had `too for it to be correct) Quote Link to comment https://forums.phpfreaks.com/topic/226677-problem-with-preg_match-have-no-clue-how-to-achieve-it/#findComment-1169905 Share on other sites More sharing options...
.josh Posted February 4, 2011 Share Posted February 4, 2011 soo...revised rules: 1) username can have 0 or 1 of !@#$%^&*() but it must be preceded by a ` 2) username can have 0 or 1 number in it, but it must be preceded by a ` 3) username can have 0 or more letters, case insensitive Quote Link to comment https://forums.phpfreaks.com/topic/226677-problem-with-preg_match-have-no-clue-how-to-achieve-it/#findComment-1169917 Share on other sites More sharing options...
Nuv Posted February 4, 2011 Author Share Posted February 4, 2011 soo...revised rules: 1) username can have 0 or 1 of !@#$%^&*() but it must be preceded by a ` 2) username can have 0 or 1 number in it, but it must be preceded by a ` 3) username can have 0 or more letters, case insensitive Yes you got it right. Quote Link to comment https://forums.phpfreaks.com/topic/226677-problem-with-preg_match-have-no-clue-how-to-achieve-it/#findComment-1169922 Share on other sites More sharing options...
.josh Posted February 4, 2011 Share Posted February 4, 2011 /* function based on the following rules: 1) username can have 0 or 1 of !@#$%^&*() but it must be preceded by a ` 2) username can have 0 or 1 number in it, but it must be preceded by a ` 3) username can have 0 or more letters, case insensitive returns true if valid, false if not */ function validateUsername($username) { // check to see if $username only contains valid chars if(!preg_match('~^[a-z0-9!@#$%^&*()`]+$~i',$username)) return false; // check if more than one number or symbol preg_match_all('~([0-9])|([!@#$%^&*()])~',$username,$matches); if ((count(array_filter($matches[1]))>1)||(count(array_filter($matches[2]))>1)) return false; // check if ` precedes symbol or number if ($matches[1]||$matches[2]) if(!preg_match('~`[0-9!@#$%^&*()]~',$username)) return false; // valide username return true; } // end validateUsername Quote Link to comment https://forums.phpfreaks.com/topic/226677-problem-with-preg_match-have-no-clue-how-to-achieve-it/#findComment-1170042 Share on other sites More sharing options...
Nuv Posted February 5, 2011 Author Share Posted February 5, 2011 /* function based on the following rules: 1) username can have 0 or 1 of !@#$%^&*() but it must be preceded by a ` 2) username can have 0 or 1 number in it, but it must be preceded by a ` 3) username can have 0 or more letters, case insensitive returns true if valid, false if not */ function validateUsername($username) { // check to see if $username only contains valid chars if(!preg_match('~^[a-z0-9!@#$%^&*()`]+$~i',$username)) return false; // check if more than one number or symbol preg_match_all('~([0-9])|([!@#$%^&*()])~',$username,$matches); if ((count(array_filter($matches[1]))>1)||(count(array_filter($matches[2]))>1)) return false; // check if ` precedes symbol or number if ($matches[1]||$matches[2]) if(!preg_match('~`[0-9!@#$%^&*()]~',$username)) return false; // valide username return true; } // end validateUsername Ah brilliant code.I was wondering though how the code would change or how the code would look like if 1) username can have 0 or more of !@#$%^&*() but it must be preceded by a ` 2) username can have 0 or more number in it, but it must be preceded by a ` 3) username can have 0 or more letters, case insensitive Quote Link to comment https://forums.phpfreaks.com/topic/226677-problem-with-preg_match-have-no-clue-how-to-achieve-it/#findComment-1170182 Share on other sites More sharing options...
Nuv Posted February 5, 2011 Author Share Posted February 5, 2011 /* function based on the following rules: 1) username can have 0 or [b]more[/b] of !@#$%^&*() but it must be preceded by a ` 2) username can have 0 or [b]more [/b]number in it, but it must be preceded by a ` 3) username can have 0 or more letters, case insensitive returns true if valid, notaccepted if not */function validateUsername($username) { // check to see if $username only contains valid chars if(!preg_match('~^[a-z0-9!@#$%^&*()`]+$~i',$username)) return notaccepted; //checking if 0 or more of [1-9!@#$%^&*()] if present, is preceded by a ` or not $counter = strlen($username); $array = str_split($username); for ($i = 0; $i <= $counter; $i++) { if($array[$i] == 1 || $array[$i] == 2 || $array[$i] == 3 || $array[$i] == 4 || $array[$i] == 5 || $array[$i] == 6 || $array[$i] == 7 || $array[$i] == 8 || $array[$i] == 9 ) { $j = $i-1; if($array[$j] != "`") { return notaccepted; } } if($array[$i] == '!' || $array[$i] == '@' || $array[$i] == '#' || $array[$i] == '%' || $array[$i] == '^' || $array[$i] == '&' || $array[$i] == '*' || $array[$i] == '(' || $array[$i] == ')' ) { $j = $i-1; if($array[$j] != "`") { return notaccepted; } } } return true; } Well above code works but im wondering if there are any intelligent/better ways to do it using preg. Quote Link to comment https://forums.phpfreaks.com/topic/226677-problem-with-preg_match-have-no-clue-how-to-achieve-it/#findComment-1170189 Share on other sites More sharing options...
.josh Posted February 5, 2011 Share Posted February 5, 2011 Ah brilliant code.I was wondering though how the code would change or how the code would look like if 1) username can have 0 or more of !@#$%^&*() but it must be preceded by a ` 2) username can have 0 or more number in it, but it must be preceded by a ` 3) username can have 0 or more letters, case insensitive /* function based on the following rules: 1) username can have 0 or more of 0-9!@#$%^&*() but it must be preceded by a ` 2) username can have 0 or more letters, case insensitive returns true if valid, false if not */ function validateUsername($username) { // check to see if $username only contains valid chars if(!preg_match('~^[a-z0-9!@#$%^&*()`]+$~i',$username)) return false; // check if ` precedes symbol or number if(!preg_match('~`[0-9!@#$%^&*()]~',$username)) return false; // valide username return true; } // end validateUsername Quote Link to comment https://forums.phpfreaks.com/topic/226677-problem-with-preg_match-have-no-clue-how-to-achieve-it/#findComment-1170246 Share on other sites More sharing options...
Nuv Posted February 5, 2011 Author Share Posted February 5, 2011 Nice...i just got stuck at '|' ..combining two patterns and didnt think of much simpler way like yours,by breaking it into many parts. Thankyou man. Quote Link to comment https://forums.phpfreaks.com/topic/226677-problem-with-preg_match-have-no-clue-how-to-achieve-it/#findComment-1170302 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.