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:
limiting input

 



seriousfun
Novice

Apr 16, 2001, 2:00 AM

Post #1 of 4 (340 views)
limiting input Can't Post

How can I limit the characters in a text input box on my form to just text and numbers. I don't want any other characters or returns. Is there a way??

thanks



sleuth
Enthusiast

Apr 16, 2001, 3:03 AM

Post #2 of 4 (338 views)
Re: limiting input [In reply to] Can't Post

 
If you want to check what they typed in for non alpa numeric characters you could use this,

use CGI qw/:standard/;
$input=param('field');

if ($input !~ /[A-Za-z0-9]/){
print "There are characters that aren't numbers or letters in here";
}

To get rid of returns,

$input=~s![\r|\n]!!g;

That's newlines & returns, from a windows pc they will be wrapped together.

Hope that helped,

Tony



japhy
Enthusiast

Apr 16, 2001, 8:33 AM

Post #3 of 4 (332 views)
Re: limiting input [In reply to] Can't Post

Your two regexes have bugs in them.

The first returns true only if the string doesn't have a single letter or number in it. To make sure a string ONLY has letters and numbers, you should do if ($str =~ /[^A-Za-z0-9]/) { bad data ... }.

The other regex uses | in a character class incorrectly. Character classes are for characters -- they are automatically using alternation. Putting the | character in a character class makes it a valid character to be matched. And removing characters is done more efficiently with tr///, like so: $str =~ tr/\n\r//d. That removes all newlines and carriage returns.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


sleuth
Enthusiast

Apr 16, 2001, 3:27 PM

Post #4 of 4 (321 views)
Re: limiting input [In reply to] Can't Post

 Sorry about that there jerky, I see I wrote that out wrong myself, I had a terribly long day yesterday,

Since I don't have much of a rep for intelligence anymore and my advice probably will be taken with a grain of salt I think I won't be coming here anymore either.

Tony


 
 


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

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