Jump to content

validating user input


DCM

Recommended Posts

Hi, what is they best way to validate user input of strings?

 

A couple of examples would be :

 

1> If i wanted to check for the existance on the coma ',' character in a string and replace it with a dash '-' character?

 

2> To check wether a user has entered a valid ip address in the form of x.x.x.x where x can range from 0-255?

 

I think i may need ereg/preg to do this but i have no idea about how to layout the syntax.

 

Thanks for looking.

Link to comment
https://forums.phpfreaks.com/topic/214859-validating-user-input/
Share on other sites


$str = str_replace(',', '-', $str); //replace commas with hyphens

if (preg_match( "/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/", $ip))
{
   echo 'Valid IP address';
}
else
{
   echo 'Invalid IP address.';
}

Thanks that works although i am having a real problem interpreting the syntax of that regular expression, need to do some backgroung reading i think on PH and regex.

 

 

 

Here is an amazing little cheat sheet to learn from. That's how I learnt. Amongst a few other sites :)

 

http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/

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.