Gossamer Forum
Home : General : Perl Programming :

how to check for http in form

Quote Reply
how to check for http in form
Hello,
Can someone tell me how to do a simple check to see if a form that is submitted that has a url field has the http:// at the beginning of that field, if not reject with an error message, saying url is not in proper format, make sure you have the http:// at the beginning of the url field.

I know how to replace things using the following:
Code:
if ($Username eq ""){

print "
<b><font color=\"red\">Error!</font></b>
Username: A unique Username is required, if your getting this error, then you didn't enter the submit ad form from an authorized location!";
exit;
}
[\pre]
But can't find in any of my perl books how to make sure that a field contains something.

Thanks
Harrison


"I've got if's pretty good, but that's about it"
Quote Reply
Re: how to check for http in form In reply to
Code:

unless ($URL =~ /^http:\/\//) {
print "Your URL must begin with http://";
}
JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: how to check for http in form In reply to
Hi JPDeni,
I tried your coding and it does in fact enforce the http:// at the beginning of the that particular form field. But it seems it requires it regardless of anything else that you put in that form field or in particular if you leave that form field blank, it wants the http:// there anyway, and what we need is to only check it if they input something so that we can be assured that the http:// is there if they put in something like www.alleycat.com and forget the http stuff.

But using that code makes the end user have to fill in at the very minimum http:// into that field which is not a good thing since it will write http:// to the mysql database and then when someone views that particular listing they will get a url link to http:// and tha'ts it.

Any suggestions?

Thanks for your time
Harrison


"I've got if's pretty good, but that's about it"
Quote Reply
Re: how to check for http in form In reply to
Code:

if ($URL) {
unless ($URL =~ /^http:\/\//) {
print "Your URL must begin with http://";
}

}
This will allow for a blank field.

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: how to check for http in form In reply to
Thanks JPDeni,
Now may Iask a question so that I fully understand what is going on with this code which does indeed work properly.


$URL =~ /^http:\/\// < I understant what the = sign means, but what does th ~ mark mean, also what does the ^ mark indicate, I assume that the ^ indicates to check at the beginning of the $url, is that correct, but I also don't understand what the ~ mark means to perl.

Does anyone know of a good perl book that lists all the possible meanings of symbols, all the perl books we have leave us desiring to learn more but don't offer a complete glossary of all the perl marks and symbols.

Thanks again
Harrison


"I've got if's pretty good, but that's about it"
Quote Reply
Re: how to check for http in form In reply to
=~ is used for pattern matching. If you have $A =~ $B it means look for $B in $A. ^ indicates the beginning of the line. A good book to learn Perl is Learning Perl (Llama book) and a good resource is Programming Perl (Camel book). Both are by O'Reilly.