...

View Full Version : preg_match validation on url - help please



devinmaking
01-05-2012, 12:11 PM
Hi huys i have a simple validation using preg_match.

There is a couple of issues which i cant seem to get right.

1. making it so: http:// to be or not to be!
2. making a /page.php (www.domain.com/page.html)

Here is the code i am using.



if(!preg_match('#^https?://[a-z0-9-._]+\.[a-z0-9]{2,4}$#i', $authorsurl)){
$errors[] = "Please Insert A Valid Website Url";
}


Thanks in advance :)

leslie.jones
01-05-2012, 01:48 PM
Probably the easiest way would be to simply not test for
https?:// if you are going to allow both of these scenarios:


http://username.abc3
https://username.abc3
username.abc3

but there may be side effects with that.

I'm guessing you are populating $authorsurl from the requested URL and I would expect the mechanism which provides it to give the http|https regardless.

Not quite sure what you mean by:

2. making a /page.php (www.domain.com/page.html)

devinmaking
01-05-2012, 02:17 PM
Probably the easiest way would be to simply not test for
https?:// if you are going to allow both of these scenarios:


http://username.abc3
https://username.abc3
username.abc3

but there may be side effects with that.

I'm guessing you are populating $authorsurl from the requested URL and I would expect the mechanism which provides it to give the http|https regardless.

Not quite sure what you mean by:

Thank you for the reply.

The url will be from users submitting comments on my blog.

and some people who link build use the http but normal people just use the www.

I suppose i can just not test for it.

The other issue which i did not come across very well is, if some posts a url/page it does not validate.

For instance...

www.domain.com/somepage.php

Because its testing for domain.com anything after this is not being allowed to be submitted.

I hope i was a little clearer now :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum