jmmorse has asked for the wisdom of the Perl Monks concerning the following question:

Have you ever noticed how some people just have to CAPITALIZE EVERY SINGLE LETTER of a post or message? I maintain a website that has Auction, Bulletin Board, and Classified Ads scripts. And It irritates the "heck" out of me to see this type of submissions. When I see them I will usually go in to the data folder and edit the Auction manually. I just thought it would be nice if there was a Javascript or Perlscript that could be setup (simply) that would automatically change the case of the post when it is submitted, no matter if they did use ALL CAPITALS. thanks. ~John
  • Comment on ChAnGiNg CaSe of forms before they are submitted?

Replies are listed 'Best First'.
Re: ChAnGiNg CaSe of forms before they are submitted?
by gav^ (Curate) on Apr 21, 2002 at 03:04 UTC
    Something like:
    while (<DATA>) { chomp; print $_, ': ', (length != tr/A-Z/A-Z/ ? 'ok' : 'not ok'), "\n"; } __DATA__ blah BLAH Blah
    Or you could use s/\b(\w)(\w+)\b/\U$1\L$2/g to force things into sentance case.

    gav^

      thanks gav! Sentence case would be great. Do I put the sentence case code in between the != and the ?
        A reply falls below the community's threshold of quality. You may see it by logging in.
      <tr><td valign=top><b>Title/Item Name:</b>No HTML</td><td><input name= +title value=\"$title\" type=text size=50 MAXLENGTH=50></td></tr> <tr><td valign=top bgcolor=#f8f0e0><font><b>Highlighted Bold Listing:< +/b></font></td><td>$bold</td></tr>

      Edit by dws to add code tags
Re: ChAnGiNg CaSe of forms before they are submitted?
by thraxil (Prior) on Apr 21, 2002 at 04:05 UTC

    try a little CSS. add this to your stylesheet:

    input, textarea {text-transform: lowercase;}

    in a browser that supports it (i know at least mozilla does; probably IE too), it won't let them enter any capital letters at all; it will convert them to lowercase as they type them in.

    it's not very flexible though. either all lowercase or nothing. still fun to use to piss off the people who want to type in all uppercase.

    anders pearson

Re: ChAnGiNg CaSe of forms before they are submitted?
by dws (Chancellor) on Apr 21, 2002 at 03:03 UTC
    Assuming you have control over the script you're using, it would be fairly easy to add a line of code that would, say, change an ALL UPPPER CASE post to all lower case. Mixed case would take a few more lines.

    Assuming you've loaded the text field into $text, try

    # if ALL CAPS (no lower case), conver to lower case if ( 0 == $text =~ tr/a-z/a-z/ ) { $text =~ tr/A-Z/a-z/; }
    I suspect, though, that you'll need to get more sophisticated, since users WHO WANT TO SHOUT can easily jinx this by adding 1 lower case character.

Re: ChAnGiNg CaSe of forms before they are submitted?
by grep (Monsignor) on Apr 21, 2002 at 07:01 UTC
    This will uc the beginning of sentences and take care of the pronoun 'I'. Sorry it won't handle proper nouns :).
    #!/usr/bin/perl use warnings; use strict; my $string = "HEY I WANT TO YELL EVERYTHING. MY MOTHER DID NOT pay ME +ENOUGH ATTENETION"; $string = join '. ',map {ucfirst} (split(/\. /,lc($string))); $string =~ s/\bi\b/I/g; $string .= '.'; print $string;
    UPDATE: Ahh... you can also repeat this to cover different ending punctuation.
    $string = join '. ',map {ucfirst} (split(/\. /,lc($string))); $string = join '? ',map {ucfirst} (split(/\? /,lc($string))); $string = join '! ',map {ucfirst} (split(/\! /,lc($string))); $string =~ s/\bi\b/I/g; $string .= '.';


    grep
    Unix - where you can thrown the manual on the keyboard and get a command
Re: ChAnGiNg CaSe of forms before they are submitted?
by beebware (Pilgrim) on Apr 21, 2002 at 12:12 UTC
    Still use the previous suggestions for how to do the job in Perl, but you might find the Javascript at http://javascript.internet.com/forms/all-lower-case.html for your liking to convert the form entry to lower case before it is submitted (remember you can't trust the Javascript to run, so just have it for "user-side viewing" and use Perl to 'force' the text to lower case).
Re: ChAnGiNg CaSe of forms before they are submitted?
by beernuts (Pilgrim) on Apr 21, 2002 at 13:59 UTC
    How about just using 'lc' if you just want everything changed to lower case?
    my $text = lc($text);

    If you were so inclined, you could even use the 'ucfirst' function to pretty things up somewhat:

    my $result; my $text = lc($text); my @words = split(/\s+/,$text); foreach(@words){ $result .= ucfirst($_)." "; }
    You could even 'ucfirst' just the first word in each sentence (although I'll leave that as an exercise to the reader). =-)


    -beernuts

Re: ChAnGiNg CaSe of forms before they are submitted?
by particle (Vicar) on Apr 21, 2002 at 02:53 UTC
    there is a process in place for this sort of thing already, although it's not automated. submit a request to Editor Requests... or is it Nodes To Consider. right now i'm not sure which is best.

    although i find it somewhat annoying, i don't think all caps should be banned. but that's what Perl Monks Discussion is for.

    Update: MY APOLOGIES!!! :) i thought you wanted to implement that here at perlmonks. on closer inspection, i found your true intent was to implement it on your own site. i'll put in an editor request to move the node to SoPW. sorry.

    to solve your problem, you'll need to... (i see it's already been addressed, so i'll stop here.)

    ~Particle ;Þ

Re: ChAnGiNg CaSe of forms before they are submitted?
by Juerd (Abbot) on Apr 22, 2002 at 13:56 UTC