Jump to content


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


form question(s)

Recommended Posts

Ok here goes...

I want to be able to stop people from registering usernames with certain characters in them (* for example), on my website by giving them an error message if they have used any of the certain characters that i would consider illegal.

Is there a way to do this?

Another similar question, but for something totally different:
I have a form that i only want the user to be able to send numbers, or they get an error message.

How would i do this?

i would be grateful if somebody could help me out on both counts :)

Thanks in advance.


Share this post

Link to post
Share on other sites
grab each input and run it through a preg_match function.

$name = $_POST['name'];


if(!preg_match("/^[a-z0-9].+$/i", $name)) {
  //error here

... go on and so sql sanity checks

Share this post

Link to post
Share on other sites
As for the 2nd question, that would depend on wether you are trying to allow any numeric value or just whole numbers. If you want any number allowed, look at the function is_numeric. If you want to check post data to make sure it is a whole number, look at the fuction ctype_digit.

It may seem like you should use is_int() to check for a whole number, but this also checks the variable type - post data is sent as a string so the function returns false on any post data.

Share this post

Link to post
Share on other sites
Another solution:

[code]function validate($str, $mask) {

  if (strspn($str, $mask) == strlen($str) && $validlength >= strlen($str)) {
      return true;
  } else {
      return false;

$str = "test";
$mask = "abcdefghijklmnopqrstuvwxyz";

The $mask string contains all of the valid characters, so you could easily change it to 0123456789 to cover a form input meant only for numbers.

This was taken from PHP.net a long time ago, but I can't remember the author's name to give proper credit :\

Share this post

Link to post
Share on other sites
This topic is now closed to further replies.


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.