Jump to content

How can i check if a field value contains only space characters?


suzzane2020

Recommended Posts

Hi ,

 

    I have a prob here that is drivin me nuts..

I am using a FCK editor and i want to validate the textarea value.

 

I want to check if the value contains only space characters and if so give an error.

I have tried the trim function but that is not working.

 

I wud really appreciate if someone cud help

 

obsidian-  its not working.still gives no error. But when i echo the string ,nothing appears :(

 

Ah, so you're actually wanting to check against HTML spaces... my bad. I would say you have to translate them first specifically for your check. Try something like this:

<?php
// return false if only spaces, true otherwise
function checkSpaces($string) {
  $new = str_replace(' ', ' ', $string);
  return empty($new)
}

$string = '    ';
if (checkSpaces($string)) {
  // invalid string... contains only spaces
}
?>

 

Hope that helps.

<?php

$string = "    ";

$chars = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY);

foreach($chars as $char) {
if($char != " ") die("The string can only contain spaces");
}

?>

 

Longwinded, but it should work.

sorry for the late response ,i was trying out all the options u gave me.

but the in the  option

 

if(ereg('^[[:space:]]*$', $string){

      echo 'error! cannot contain just spaces';

}

 

the prob is that it checks for only one space character

 

just not working :(

if(ereg('^[[:space:]]*$', $string){
      echo 'error! cannot contain just spaces';
}

should check for one or more space. the * symbol means one or more..

try:

if(ereg('^([[:space:]]*)$', $string){
      echo 'error! cannot contain just spaces';
}

I've just tried this exact code and it works fine for me.. although it doesn't check for the actual html ' ' it just checks for one or more whitespaces.

 

<?php
if(isset($_POST['submit'])){ //if post is submit then...
if(ereg('^[[:space:]]*$', $_POST['string'])){
      	echo 'error! cannot contain just spaces';
}else{
	echo 'correct!';
}
}else{
?>
<form name="staffedit" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="text" name="string" value="" />
<input type="submit" name="submit" value="submit" />
</form>
<?php
}
?>

<?php
eregi(' *', $string);
?>

 

That will match if any non breaking spaces are found. It would also match &nbsp or  ;;;;.

 

Suzanne, did you try the method that effigy and I have both suggested? Basically, you'll want to replace all your ' ' characters with "real" spaces, then compare your string. If it still contains only spaces, you know you've got a problem. Here's a commented summary:

<?php
// Define an array of all HTML characters you want to consider spaces
$spaces = array(' ');

// first, create a comparable string by changing all HTML characters in your list to real spaces
$comp_string = str_replace($spaces, ' ', $string);

// Now, simply make sure that the string isn't spaces from beginning to end:
if (preg_match('|^\s*\z|', $comp_string)) {
  // Only contains spaces
}
?>

 

Hope that helps

  • 2 years later...

$string = '                  abra cadabra      ';// check characters with spaces

$string = '                        '; // check only whitespaces

function TrimStr($str)

{

$str = trim($str);

for($i=0;$i < strlen($str);$i++)

{

 

if(substr($str, $i, 1) != " ")

{

 

$ret_str .= trim(substr($str, $i, 1));

 

}

else

{

while(substr($str,$i,1) == " ")

 

{

$i++;

}

$ret_str.= " ";

$i--; // ***

}

}

return $ret_str;

}

 

$newstring = TrimStr($string) ;

echo "<BR>\$newstring is: " . $newstring;

 

if($newstring <1){

echo "<BR>\$string had just spaces in it!";

}

Sorry, I posted an error... since a string could numerically be zero, the code should be:

$string = '                  abra cadabra      ';// check characters with spaces

$string = '          000              '; // check only whitespaces

function TrimStr($str)

{

$str = trim($str);

for($i=0;$i < strlen($str);$i++)

{

 

if(substr($str, $i, 1) != " ")

{

 

$ret_str .= trim(substr($str, $i, 1));

 

}

else

{

while(substr($str,$i,1) == " ")

 

{

$i++;

}

$ret_str.= " ";

$i--; // ***

}

}

return $ret_str;

}

 

$newstring = TrimStr($string) ;

echo "<BR>\$newstring is: " . $newstring;

 

if($newstring == NULL){

echo "<BR>\$string had just spaces in it!";

}

Are you the original poster also or?... dunno but here is some variations.

 

<?php
// Remove html entity spaces and normal spaces.
$string = "               ";
$string = trim(str_replace(' ', '', $string));
$msg = !strlen($string) ? 'Empty string.' : '';
echo $msg; // Empty string.

// Remove leading and following spaces.
$str = '       000       ';
echo trim($str); // "000"

// Same as above.
$str = '                  abra cadabra      ';
echo trim($str); // abra cadabra 

// Replace all spaces in string.
$str = '                  abra cadabra      ';
echo str_replace(' ', '', $str); // abracadabra 

// Find if string had atleast one space in it.
if (preg_match('/\s/', $str, $matches))
{
echo 'String contains space(s).';
}

You need to examine the raw output (or input) from the control to determine exactly what you need to remove.  There could be empty paragraph tags, br tags, or all sorts of other crap submitted by a rich edit control that you'll have to handle.

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.