Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use of ALL CAPS

Status
Not open for further replies.

bnc123

Technical User
Mar 25, 2001
59
AU
I would like to discourage the usage of ALLCAPS on my HTML forms.

How do I detect using perl, if someone has used ALL CAPITALS for a form field input?

Thanks for your help.
 
I'm assuming that when you say ALL CAPS you mean that all the alpha chars are uppercase; spaces, digits, punctuation, etc. don't count. In other words, the string contains uppercase chars and no lowercase chars. I think this does what you want.
Code:
#!perl
use strict;
use warnings;

[b]sub allcaps {
    # Returns true if string contains uppercase chars and
    # no lowercase chars, false otherwise.
    my $str = shift;
    $str =~ /[[:upper:]]/ && $str !~ /[[:lower:]]/;
}[/b]

while (<DATA>) {
    chomp;
    print "$_>> ", allcaps2($_)? "": "Not ", "All Caps\n";
}
 
__DATA__
I would like to discourage the usage of ALLCAPS.
How do I detect using perl, if someone has used ALL CAPITALS
ALL CAPITALS
for a form field input?
Thanks for your help.
HTML
1234
abc123
123ABC
ALL 365 CAPS 123

[b]Output[/b]
I would like to discourage the usage of ALLCAPS.>> [b]Not All Caps[/b]
How do I detect using perl, if someone has used ALL CAPITALS>> [b]Not All Caps[/b]
ALL CAPITALS>> [b]All Caps[/b]
for a form field input?>> [b]Not All Caps[/b]
Thanks for your help.>> [b]Not All Caps[/b]
HTML>> [b]All Caps[/b]
1234>> [b]Not All Caps[/b]
abc123>> [b]Not All Caps[/b]
123ABC>> [b]All Caps[/b]
ALL 365 CAPS 123>> [b]All Caps[/b]
 
Code:
$STRING="AAA";
print $a=($STRING=~s/([A-Z][A-Z]+)/\L$1/), "\n";

Test the string can be changed to lower case or not. If yes, $a is 1 else is 0. Just a reference for you.

tikual
 
I like the idea, tikual, but as it stands there's a bug in this. What if $STRING is "A" or "ABC123DEF456"? Those are uppercase, aren't they? Also, this doesn't return 0 when it fails, as you say it does.

It can be fixed pretty easily. The problem is the re. See it? :)
 
Thanks mikevh and tikual for your inputs.

Since I have not yet mastered subroutines, I am finding mikhev's answer to my question, a bit difficult to incorporate into my formmail script.

tikual's answer on the other hand, is quite simple and straightforward for a newbie like me.

Thanks again.


 
Then forget about using a sub and just use the
$str =~ /[[:upper:]]/ && $str !~ /[[:lower:]]/;
part.

if ($str =~ /[[:upper:]]/ && $str !~ /[[:lower:]]/) {
# it's all caps
} else {
# it's not all caps
}


tikual's solution as posted contains a bug.
(By the way, I meant "A123B456", not "ABC123DEF456".
tikual's solution will return false [not uc] for this string, as it will for "A".)



 
I tried tikuals method with $string being "ABC123DEF456". I had no problems.

My little script looks like this:




#!/usr/bin/perl

$STRING="ABC123DEF456";
print $a=($STRING=~s/([A-Z][A-Z]+)/\L$1/), "\n";

if ($a == 1)
{
print "Content-type: text/html\n\n";
print "<html><h1>All Caps</h1></html>\n";
}

else
{
print "Content-type: text/html\n\n";
print "<html><h1>Not All Caps</h1></html>\n";
}


 
Read my last post. Try tikual's method with "A" or
"A123B345", or "A B C", or any string that has only one uppercase letter in a row.
 
I see what you mean mikevh. I have now deployed your method in my little script. I have always been very confused by subroutines. But I'll get there one day.

Thanks.
 
Don't put it off too long. Understanding subroutines is truly one of the sine qua non's of programming. (Along with a knowledge of Latin phrases.) [wink]
 
Hi all,

Because regular expression is another big topic. Everyone have different situations. Just posted my idea here but not a solution. ^^

Have fun in Perl

tikual
 
tikual, the problem was your re:
/([A-Z][A-Z]+)/\L$1/
Get rid of the first [A-Z], or change the + to a *, and it should work.
As you posted it, it will only work on strings that contain at least 2 uppercase characters in a row.
 
Hi mikevh,

Because bnc123 said 'all caps for a form field', so I don't think I need to match the case such as sex is 'M' or 'F' in the form field. I know that my post was poor. Just let him customize it for himself. Anyway, thanks for your guidance. ^^

tikual
 
So where would I find a few tutorials on subroutines?
 
It was very late when I did those last posts. Now that I've had a few hours sleep, I can see that even with my suggested modification, your solution does not work correctly, tikual. It will return true if there are ANY uppercase characters in the string at all, not just when the string is ALL uppercase. Observe:
Code:
#!perl
use strict;
use warnings;

my @strings = (
    'This is a string.',
    'this is a string.',
    'this is a String.',
    'Hey there.',
    'hOWzit?',
    'e. e. cummings',
    'e. e. Cummings',
);

for (@strings) {
    my $STRING = $_;
    my $a=($STRING=~s/([A-Z]+)/\L$1/);
    print "$_: ", $a? "All upper": "Not all upper", "\n";
}

[b]Output:[/b]
This is a string.: [b]All upper[/b]
this is a string.: [b]Not all upper[/b]
this is a String.: [b]All upper[/b]
Hey there.: [b]All upper[/b]
hOWzit?: [b]All upper[/b]
e. e. cummings: [b]Not all upper[/b]
e. e. Cummings: [b]All upper[/b]
What you need to do is test that there are uppercase chars, as you have done, and test that there are no lowercase chars, which you don't do. In other words,
/[A-Z]/ && !/[a-z]/, or /[[:upper:]]/ && !/[[:lower:]]/.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top