EchoFool Posted May 7, 2010 Share Posted May 7, 2010 Hey, Does any one know the regex to only allow the string to be letters only with no spaces/numbers/special characters? Hope you can help as i use: function textonly($Text){ return(preg_match('!^[^a-zA-Z]+$!', $Text)); } $Name = SQLskip($_POST['name']); $Name = str_replace(' ','', $Name); $Name = strtolower($Name); If(textonly($Name) != 1){ //BAD }Else{ If(strlen($Name)>15 OR strlen($Name)<1){ //BAD }Else{ //GOOD } } Yet some how it doesn't work as some one just managed to enter a blank name or it was a name with lots of spacebar presses i couldn't tell ... hope you can help Quote Link to comment Share on other sites More sharing options...
Alex Posted May 7, 2010 Share Posted May 7, 2010 When possible you should avoid using the regex engine as it uses a lot of resources. In this case you should use ctype_alpha. Quote Link to comment Share on other sites More sharing options...
EchoFool Posted May 7, 2010 Author Share Posted May 7, 2010 Thanks! Also i use a similiar thing to check a inputted number is not decimal and is integer is there a simpler function than: //integer input check function preg($Var){ If(!(preg_match('~^\d+$~', $Var))) { return(0); }Else{ return($Var); } } Quote Link to comment Share on other sites More sharing options...
ignace Posted May 8, 2010 Share Posted May 8, 2010 What do you mean by is not decimal? not float? but is_integer? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted May 8, 2010 Share Posted May 8, 2010 What do you mean by is not decimal? not float? but is_integer? is_int() checks if the data type is an integer. He is getting input from input from a user, so the data type will always be a string. Quote Link to comment Share on other sites More sharing options...
litebearer Posted May 8, 2010 Share Posted May 8, 2010 Caution - Danger Old Man Thinking (NOT tested but...) Psuedo Code: $var_to_check = the variable you are check to see if it is (A) a valid number and (B) an integer function Val_the_var($var_to_check) { FIRST check if it is a valid number of any type ie int, float etc if (is_numeric ($val_to_check)) { IF YES create a test variable to remove everything but the number 0-9 $test_val_1 = preg_replace("/[^0-9]/","", $var_to_check); COMPARE the two values to see if they are the same IF($test_val_1 === $var_to_check) { IT IS a valid integer return a value of 2 }else{ IT IS A number BUT NOT an integer return a value of 1 } else { IF NO return a value of zero } } $what_to_do = Val_the_var($var_to_check); IF $what_to_do = 0 (meaning NOT a number) decide how you want to handle IF $what_to_do = 1 (meaning a number BUT NOT an integer) decide how you want to handle IF $what_to_do = 2 (meaning A VALID integer) decide how you want to handle make sense? Quote Link to comment Share on other sites More sharing options...
EchoFool Posted May 8, 2010 Author Share Posted May 8, 2010 What do you mean by is not decimal? not float? but is_integer? is_int() checks if the data type is an integer. He is getting input from input from a user, so the data type will always be a string. Does that mean is_int() will return false as the input is a string and not a number? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted May 8, 2010 Share Posted May 8, 2010 What do you mean by is not decimal? not float? but is_integer? is_int() checks if the data type is an integer. He is getting input from input from a user, so the data type will always be a string. Does that mean is_int() will return false as the input is a string and not a number? Yes: daniel@daniel-laptop:~$ php -r 'var_dump(is_int("123"));' bool(false) Quote Link to comment Share on other sites More sharing options...
EchoFool Posted May 8, 2010 Author Share Posted May 8, 2010 Hmm so i can't use is_int if 123 returns false then ... =/ id have to use my current preg_match ? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted May 8, 2010 Share Posted May 8, 2010 Yes, doing like this should be fine for checking if it's an integer: if (preg_match('#^-?\d+$#', $theInteger)) { // is int } Quote Link to comment Share on other sites More sharing options...
EchoFool Posted May 8, 2010 Author Share Posted May 8, 2010 Oh your preg_match is different to mine, is that because mine is wrong or theres more than one way to do the same thing? Quote Link to comment Share on other sites More sharing options...
Andy-H Posted May 8, 2010 Share Posted May 8, 2010 ctype_digit is_numeric Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted May 8, 2010 Share Posted May 8, 2010 Oh your preg_match is different to mine, is that because mine is wrong or theres more than one way to do the same thing? The regex I provided allows negative integers. ctype_digit is_numeric ctype_digit() doesn't allow negative integers. is_numeric() allows floats. Quote Link to comment Share on other sites More sharing options...
EchoFool Posted May 9, 2010 Author Share Posted May 9, 2010 Okay so ctypt_digit seems the best function choice over preg_match Quote Link to comment Share on other sites More sharing options...
niranjan81 Posted May 9, 2010 Share Posted May 9, 2010 Or if negative integers are welcome as well you can use validate integer flag while filtering the input, its nice, since its just then n there, not even a line more Quote Link to comment 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.