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
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.';
}

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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.