Home     Articles & Projects     Products & Web Services

Find if string has symbols

Hi I want to find if a user has placed a symbol or symbols in a input field.
If they have I want to show an error.
code $109

However I am not sure how I do it.

Hi Russell, You can always

Hi Russell,

You can always use strpos to search for unwanted characters within an input field, e.g. the % character:

  $field = $_POST["field"];
  if (strpos($field,"%")!==FALSE)
  {
    // set-up your error handling here
  }

However, a more common approach within PHP is to sanitise form input fields using regular expressions to ensure that they only contain characters within specified bounds, such as a-Z, A-Z, 0-9 etc, and anything outside of that you generate an error.

Considering that example, have a look at the following;

  $field = $_POST["field"];
  if (!ereg("^[0-9a-zA-Z]{1,255}$",$field)
  {
    // set-up your error handling here
  } 

The above would permit any characters 0-9,a-z,A-Z between 1 and 255 characters in length...

Thank You, works like a

Thank You, works like a charm.

No probs, Russell. Regarding

No probs, Russell.

Regarding your other post, I can only really post / respond to specific questions relating to a very small snippet of code, or specific question that's all i'm afraid!

Thanks for your understanding;
Cheers,
David.

Oh ok, is there away you

Oh ok, is there away you could do a post on OOP mySQL select
the part I am having issues with is the following part, as it is not showing the results.

{code saved}

I was able to make it 100%

I was able to make it 100% shorter and got it working.
i just removed the checking code which makes sure the table is there etc.