Jump to content

Recommended Posts

If a user enters data into an input field, i do not want them to be able to upload special characreters like the following or numbers:

 

  • "- #'][{}£$%^&*()â▀♣

 

and many more from character map.

 

how can i do this, by checking a variable like...

 

$username = $_POST['username'];

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/189080-special-characters-and-symbol-problem/
Share on other sites

Can you manage letters only??

 

function alpha_only($string)

    {

        return (preg_match("/[A-Z\s_]/i", $string) > 0) ? true : false;

    }

 

That function will not work. It simply returns true if ANY ONE character matches the pattern! The pattern needs to be exclusionary (Besides, why would you test if something is true, just to return true or false? Just return the result of the test. See example below)

 

function validCharacters($input)
{
    //Returns true if all the characters are letters (upper and lower case)
    return (preg_match("/[^\w]/", $input)==0);
}

//Usage
if (!validCharacters($_POST['username']))
{
  echo "Username can only contain letters";
}
else
{
  //Input was valid
}

The following function only works for the variable "$forname" how can i get it to also work for $surname and $city

 

function validCharacters($input)
   {
      //Returns true if all the characters are letters (upper and lower case)
      return (preg_match("/[^\w]/", $input)==0);
   }

   //Usage
   if (!validCharacters($forname)+($surname)+($city))
    {
      echo "All boxes can only contain letters";
    }
      else
   {
      echo "success";
   }

Typically you would validate each field individually and give the user specific details about what fields need to be changed, but if you want to do it all in one go, then use the correct method for concatenating string - which is a period:

 

if (!validCharacters($forname.$surname.$city))

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.