CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
form validation

 



Calvin
stranger

Jul 25, 2001, 11:01 PM

Post #1 of 10 (2604 views)
form validation Can't Post

I have this code:

if ($vars_gen{'Field2Data'} eq "url") {
if (($in{'Field2'} eq "") || ($in{'Field2'} !~ /http:\/\/.*?\.\w+/)) {
&GBStandardHTML("You have entered an invalid URL for the $vars_gen{'Field2Name'} field. Your URL must include the http://.");
exit;
}
}

The code checks if a valid email is entered and if http:// is there. What I want it to do is to not display any errors if someone didn't enter anything in the field. If they did, the script will check to see if http:// is there.

Can someone fix that for me.

Thank you.



veredox
stranger

Jul 26, 2001, 12:11 AM

Post #2 of 10 (2601 views)
Re: form validation [In reply to] Can't Post

if ($vars_gen{'Field2Data'} eq "url") {
if (!($in{'Field2'} eq "") && ($in{'Field2'} !~ /http:\/\/.*?\.\w+/)) {
&GBStandardHTML("You have entered an invalid URL for the $vars_gen{'Field2Name'} field. Your URL must include the http://.");
exit;
}
}

Try that.

Matt Murphy



Calvin
stranger

Jul 26, 2001, 1:29 AM

Post #3 of 10 (2599 views)
Re: form validation [In reply to] Can't Post

No it did not work. :(



dsb
stranger

Jul 26, 2001, 8:52 AM

Post #4 of 10 (2595 views)
Re: form validation [In reply to] Can't Post

Learn how to markup your Posts

As for you code:

Code
if ($vars_gen{'Field2Data'} eq "url") { 
if (($in{'Field2'} eq "") || ($in{'Field2'} !~ /http:\/\/.*?\.\w+/)) {
&GBStandardHTML("You have entered an invalid URL for the $vars_gen{'Field2Name'} field. Your URL must include the http://.");
exit;
}
}

You need to split your one conditional statement into two.

Code
if ($vars_gen{'Field2Data'} eq "url") { 
if ($in{'Field2'} eq "") {
# no-entry processing
} elsif ($in{'Field2'} !~ /http:\/\/.*?\.\w+/) {
&GBStandardHTML("You have entered an invalid URL for the $vars_gen{'Field2Name'} field. Your URL must include the http://.");
exit;
}
}

dan Wink


Calvin
stranger

Jul 26, 2001, 5:12 PM

Post #5 of 10 (2578 views)
Re: form validation [In reply to] Can't Post

It STILL didn't work. :(

If you want FTP access I can make you an account. What's your email address and I'll email you.



Calvin
stranger

Jul 27, 2001, 12:36 PM

Post #6 of 10 (2564 views)
Re: form validation [In reply to] Can't Post

^



Calvin
stranger

Aug 2, 2001, 12:57 AM

Post #7 of 10 (2553 views)
Re: form validation [In reply to] Can't Post

^ ^



mhx
Enthusiast / Moderator

Aug 2, 2001, 3:37 AM

Post #8 of 10 (2550 views)
Re: form validation [In reply to] Can't Post

Ok, the following code should work:

Code
#!/bin/perl -w 
use strict;

my %vars_gen = (
Field2Data => 'url',
Field2Name => 'Whatever URL',
);

my %in = (
Field2 => 'http://www.gmx.de/test',
);

if( $vars_gen{Field2Data} eq 'url' &&
$in{Field2} !~ m[^\s*$|^http://\w+(?:\.\w+)+] ) {
print "Invalid URL in $vars_gen{Field2Name}: $in{Field2}";
exit;
}

Now, the user may enter nothing, only whitespace, or a 'valid' URL.
I want to stress here that URI detection / verification is a lot more complicated than just matching [http://\w+(?:\.\w+)+]. I'm currently writing a URI detection / verification / filtering module, and the main regex looks like this:

Code
my $URI = <<'REGEX'; 
((?:([a-zA-Z][-+.\w]*):)(?:((?:(//((?:((?:((?:[-_.!~*'()\w;:&=+$,]|%[0
-9a-fA-F]{2})+)@)?((?:((?:\w(?:[\w-]*\w)?\.)+([a-zA-Z](?:[\w-]*\w)?))|
((?:[01]?\d\d?|2[0-4]\d|25[0-5])\.(?:[01]?\d\d?|2[0-4]\d|25[0-5])\.(?:
[01]?\d\d?|2[0-4]\d|25[0-5])\.(?:[01]?\d\d?|2[0-4]\d|25[0-5]))))(?::(\
d*))?)|((?:[-_.!~*'()\w$,;:@&=+]|%[0-9a-fA-F]{2})+)))(/(?:[-_.!~*'()\w
:@&=+$,]|%[0-9a-fA-F]{2})*(?:;(?:[-_.!~*'()\w:@&=+$,]|%[0-9a-fA-F]{2})
*)*(?:/(?:[-_.!~*'()\w:@&=+$,]|%[0-9a-fA-F]{2})*(?:;(?:[-_.!~*'()\w:@&
=+$,]|%[0-9a-fA-F]{2})*)*)*)?)|(/(?:[-_.!~*'()\w:@&=+$,]|%[0-9a-fA-F]{
2})*(?:;(?:[-_.!~*'()\w:@&=+$,]|%[0-9a-fA-F]{2})*)*(?:/(?:[-_.!~*'()\w
:@&=+$,]|%[0-9a-fA-F]{2})*(?:;(?:[-_.!~*'()\w:@&=+$,]|%[0-9a-fA-F]{2})
*)*)*))(?:\?((?:[-_.!~*'()\w;/?:@&=+$,]|%[0-9a-fA-F]{2})*))?)|((?:[-_.
!~*'()\w;?:@&=+$,]|%[0-9a-fA-F]{2})(?:[-_.!~*'()\w;/?:@&=+$,]|%[0-9a-f
A-F]{2})*)))(?:\#((?:[-_.!~*'()\w;/?:@&=+$,]|%[0-9a-fA-F]{2})*))?
REGEX

And this regex itself of course doesn't ensure that the URI does exist.
Hope this helps. (Forget the regex, don't try to understand it ;-)

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



Calvin
stranger

Aug 2, 2001, 11:45 AM

Post #9 of 10 (2540 views)
Re: form validation [In reply to] Can't Post

It didn't work. :(

It didn't work when I entered nothing in the field. I didn't put use strict; on my script. Do I have to put that?



mhx
Enthusiast / Moderator

Aug 2, 2001, 12:32 PM

Post #10 of 10 (2538 views)
Re: form validation [In reply to] Can't Post

It didn't work. That's cool. Now I know exactly what's your problem. By the way, my car doesn't drive. Could you tell me why?

Ok, I have tested the code with several strings before I posted it. If the string was empty or contained only whitespace characters, no error was issued. If the string matched a http URL, no error was issued. For all other strings, an error message was printed.

If you entered nothing in your field, then $in{Field2} should contain the empty string value. If this is the case, no error is issued. That's exactly the behaviour you described in your first post. If you should get an error message if you entered nothing, then $in{Field2} does contain something else than the empty string or just whitespace. You should figure out the content of $in{Field2}.

For the use strict;, it's better if you put it in, but it's not neccessary. But you should at least use the -w switch.

Sorry, but that's all I can tell you right now.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives