Jump to content


Photo

form question(s)


  • This topic is locked This topic is locked
4 replies to this topic

#1 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 16 July 2006 - 06:35 AM

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.

-Tandem

#2 rab

rab
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts

Posted 16 July 2006 - 07:51 AM

grab each input and run it through a preg_match function.

IE.
$name = $_POST['name'];

...

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

... go on and so sql sanity checks


#3 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 16 July 2006 - 09:54 AM

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.

#4 athmpsn

athmpsn
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 16 July 2006 - 10:03 AM

Another solution:

function validate($str, $mask) {
   $validlength=10;

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

$str = "test";
$mask = "abcdefghijklmnopqrstuvwxyz";
validate($str,$mask);

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 :\

#5 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 16 July 2006 - 01:44 PM

Awesome, thankyou.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users