Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Parse an URL

by Anonymous Monk
on Apr 09, 2002 at 06:56 UTC ( #157668=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

When a form is sent I need some Java Script code that will prohibit the form from being sent unless all the fields are non-empty and the fields that hold URLs either hold valid URLs or hold a partial URL with no protocol mentioned. Minimally I mean strings whose first non-blank characters are http:// or ftp:// where case is not an issue. This means that Iwould consider both HttP:// and as valid. I also would allow for no mention of the protocol as in Also, some Java Script that will place http:// in a field if someone types a partial URL with no protocol. So if someone were to type in one of the form items that is meant to hold a URL, then it is replaced with http://www1.gmu,edu when the cursor is moved from the form element.

Replies are listed 'Best First'.
Re: Parse an URL
by rjray (Chaplain) on Apr 09, 2002 at 07:16 UTC

    I'm afraid that the first thing you need to do is find a JavaScript forum to take your problem to. This is a Perl forum, and I can't help but suspect that those who reply after me won't be quite as polite.

    That having been said, if you come to the realization that you're better-off doing the sanity check in a cleaner language, and simply re-painting the form with badly-formed fields noted (red is usually a good color for this sort of thing) while carrying over the valid form fields as "sticky" values, then you will have the luxury of using URI, which is a very clean, easy-to-use module that should handle more URI issues than you're likely to throw at it.


Re: Parse an URL
by grinder (Bishop) on Apr 09, 2002 at 07:31 UTC

    rjray speaks wisely. The only thing I would add is that all your funky javascript code will be for nought, should the user happen to have javascript deactivated.

    A second possibility is that someone will pick apart your form and figure out what values the server is looking for, and craft their own response, completely bypassing the validations the javascript code performs.

    Never, ever trust anything coming from the client. Buried in a recent thread on web site design (or lack thereof) are some pointers to some very good sources of information that it would behoove you to read.

    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
Re: Parse an URL
by tachyon (Chancellor) on Apr 09, 2002 at 12:15 UTC

    You will have to do your own work and learn about pattern matching regexes then read the relevant RFCs to determine what constitutes a valid URL. This snippet basically shows you how you do it but the only valid URL is

    <head> <script> function check(url) { if (url.match("(http|ftp)://") ) { return 1; } else { alert(url+" is not a valid URL\nPlease enter a valid one") my_form.url.value = 'http://'; return 0; } } function check_fields(form) { // check your form fields here alert("You need to revalidate server side! use Perl;"); return 1; } </script> </head> <body> <form name="my_form" method=GET action="/do/this/stuff.cgi" onSubmit=" +check_fields(my_form)"> <p><input type="text" name="url" size=25 maxlength=100 value="http://" + onMouseOut="check(url.value)"> <p><input type="submit" value="Stumbit"> </form> </body> </html>




      There is an excellent perl module for URI-parsing. Imho having everyone invent his own URL-parser is unnecessary work. You might take a look at:

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://157668]
Approved by strat
[TCLion]: DateTime::Format:: Flexible seems to have a lot of dependents, taking a while to install

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2017-03-23 15:21 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (289 votes). Check out past polls.