By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,625 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,625 IT Pros & Developers. It's quick & easy.

Need a regex to check form submission url format

P: 2
I want a simple as possible regex to check the format of my form submission URL string, I don't care whether is an actual real working URL, just that it be in proper URL format and, IF POSSIBLE add someting so that the URL characters are the only charactrers entered in the field:
so it would be an err msg prompt for legit users to enter their link in the proper format;
On the uther hand, it should not allow spammers to enter tons of text in the url field.
I have used the following regex to chewck that there are characters supplied in addition to the http:// value in the form field, and it works with simple urls but it does not work with urls such as
http://somesubdomain-somesitename.org/somepagename.htm
if ($FORM{'url'} eq 'http://' || $FORM{'url'} !~ /^(f|ht)tp:\/\/\w+\.\w+/) {
&no_url;
}
Nov 13 '08 #1
Share this Question
Share on Google+
2 Replies


KevinADC
Expert 2.5K+
P: 4,059
There are a number of URI checking modules on CPAN, but if you don't want to go the module route, you can use this regexp taken from the URI modules documentation:

Expand|Select|Wrap|Line Numbers
  1. my $uri = 'http://www.mysite.com:8080/path/index.html?test=foo&foo=bar#internal-link';
  2.  
  3. my($scheme, $authority, $path, $query, $fragment) =
  4.   $uri =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|;
  5.  
  6. print qq{scheme = $scheme
  7. authority = $authority
  8. path = $path
  9. query = $query
  10. fragment = $fragment};
  11.  
Then you apply tests to each part of the URI individually.
Nov 13 '08 #2

P: 2
Thx for the response.
This reply delayed as I had to go out of town.
My thought was that what I really need to do is check for basic url format in the form submissions.
As any valid url format has to be OK, then, manual editing or other spam checks in the script is the spam solution, as just checking for basic url formating is the key here.

I came up with if string != (http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?) {
& check url;
}
which works for simple urls, but I'd like to add code for more complex urls such as http://subdomain.somedomain.com/somefilename.htmlor
http://xyzabd-somedomain.com/somefilename.html
or
Seems simple (to me) but not able to to get the script to process - it hangs on the url field data.
thx,
Mike
Nov 17 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.