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
Share on other sites

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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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