Jump to content

[SOLVED] Validating a MySpace-style URL.


DavidGS

Recommended Posts

You know how when you register a MySpace account you can choose a personal URL? Basically I am making a social networking website and I am adding the same feature. The only problem is how would I validate a URL the person registering has entered to make sure it doesn't contain any characters that could case problems? Like, I wouldn't want the person entering @ because that would be interpreted as an e-mail address or . because that would be mistaken for a file type separator. I want to validate it to make sure it only contains letters, numbers and underscores. Anyone have an expression I could use?

Link to comment
Share on other sites

What exactly do you want to have in this input? If it's simply "only letters number and underscores" you'd do something like this:

if(preg_match("/[a-z0-9_]+/i", $input)) ...

 

But if you want to check if the input is a valid myspace url, you'll have to do something more specific than that. Define exactly what is valid and what is not. Give a few examples too.

 

I once needed to validate if an inputted url was a valid myspace profile url. I don't know if that's what you are looking for. But if it is, this is what I did (first expression deals with urls that have nicknames, the second one deals with those that have the ID in them):

$regex1 = "|^(http:\/\/){0,1}(www\.){0,1}myspace\.com\/[a-z0-9_]+$|i";
$regex2 = "|^(http:\/\/){0,1}profile\.myspace\.com\/index\.cfm\?fuseaction=user\.viewprofile&friendID=[0-9]+$|i";

 

 

Orio.

Link to comment
Share on other sites

Orio,

 

Basically the user can put in anything they like as long as it doesn't have certain characters in it.

 

Example, they register with the URL "joe", their personal URL would be http://www.website.com/joe/

 

Because this will be an actual URL I don't want characters than can cause any errors when trying to access it.

 

So by limiting what the user can select as his personal URL I can avoid any errors

 

The user will be able to enter letters, numbers and underscores and them only.

 

They register with the URL "joe_bob6", their personal URL would be http://www.website.com/joe_bob6/

 

That would be an acceptable URL and there would be no issue accessing it.

 

But if the user registered with the URL "$joe%bob/", their URL would be http://www.website.com/$joe%bob// which may be problematic and cause an interpretation error in PHP (particularly with the dollar sign).

 

This is so that can be avoided.

 

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.