Jump to content

Accept only letters


EchoFool

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.