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 Link to comment https://forums.phpfreaks.com/topic/201061-accept-only-letters/ 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. Link to comment https://forums.phpfreaks.com/topic/201061-accept-only-letters/#findComment-1054876 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); } } Link to comment https://forums.phpfreaks.com/topic/201061-accept-only-letters/#findComment-1054878 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? Link to comment https://forums.phpfreaks.com/topic/201061-accept-only-letters/#findComment-1054943 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. Link to comment https://forums.phpfreaks.com/topic/201061-accept-only-letters/#findComment-1055043 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? Link to comment https://forums.phpfreaks.com/topic/201061-accept-only-letters/#findComment-1055050 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? Link to comment https://forums.phpfreaks.com/topic/201061-accept-only-letters/#findComment-1055069 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) Link to comment https://forums.phpfreaks.com/topic/201061-accept-only-letters/#findComment-1055070 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 ? Link to comment https://forums.phpfreaks.com/topic/201061-accept-only-letters/#findComment-1055071 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 } Link to comment https://forums.phpfreaks.com/topic/201061-accept-only-letters/#findComment-1055075 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? Link to comment https://forums.phpfreaks.com/topic/201061-accept-only-letters/#findComment-1055076 Share on other sites More sharing options...
Andy-H Posted May 8, 2010 Share Posted May 8, 2010 ctype_digit is_numeric Link to comment https://forums.phpfreaks.com/topic/201061-accept-only-letters/#findComment-1055078 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. Link to comment https://forums.phpfreaks.com/topic/201061-accept-only-letters/#findComment-1055081 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 Link to comment https://forums.phpfreaks.com/topic/201061-accept-only-letters/#findComment-1055245 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 Link to comment https://forums.phpfreaks.com/topic/201061-accept-only-letters/#findComment-1055252 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.