Automatic Capitalization of the Text in a Form

Discussion in 'HTML & Website Design' started by yfs1, Aug 29, 2005.

  1. #1
    Basically the title of the thread says it all. I am looking for any info (providing links is fine as I can't seem to find it via Google) on incorporating automatic capitalization in one field on a form.

    Basically if the submitter fills in
    the quick brown fox is in the field
    The Quick brown fox is in the field
    THE QUICK BROWN FOX IS IN THE FIELD

    it should be submitted as

    The Quick Brown Fox is in the Field

    Anything to get me started?
     
    yfs1, Aug 29, 2005 IP
  2. ResaleBroker

    ResaleBroker Active Member

    Messages:
    1,665
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    90
    #2
    ResaleBroker, Aug 29, 2005 IP
  3. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That would be for display only though, right?

    I don't think that will help me as the results of the form are used places (like emails, raw code) where CSS won't help.
     
    yfs1, Aug 29, 2005 IP
  4. BigTicket

    BigTicket Peon

    Messages:
    197
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I am assuming you are using php? If so this should do what you need.

    $text = ucwords(strtolower($text));
     
    BigTicket, Aug 29, 2005 IP
  5. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #5
    Do you want to do this server side via PHP or client side via Javascript?
     
    dct, Aug 29, 2005 IP
  6. tresman

    tresman Well-Known Member

    Messages:
    235
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    138
    #6
    Your are right, it would be only for display. That's what CSS is about.

    In your case, what you should do is use some server side programming to transform the text before inserting it in, let's say, a database. Javascript could help, but won't as secure as doing it on the other side (the server).
     
    tresman, Aug 29, 2005 IP
  7. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thats exactly what I want to do...The user enters it however they like and then its converted before being written to the mySQL database.
     
    yfs1, Aug 29, 2005 IP
  8. Perrow

    Perrow Well-Known Member

    Messages:
    1,306
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    140
    #8
    If you want to keep certain words all lower case you'll need some more coding. A quick pseudo code suggestion.
    DontWords  = Split("in|of|theis|...)
    OriginalString = split(tolower(form("field")), " ")
    
    for each strWord in OriginalString
      if strWord exists in DontWords then
        dont capitalise
        result = result + " " + strWord
      else
        capitalise strWord  // see above post
        result = result + "" + strWord
      end if
    next
    result = trim(result)  //shave off any extra spaces
    Code (markup):
    Where "dontWords" prefferably is some sort of dictionary object that has the exists function built in (or a custom built function DontWord(word)).
     
    Perrow, Aug 29, 2005 IP
  9. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #9
    This is basically where I am writing to the database which is where I assume I can do this where entry2 needs to have the rule applied


    <?php
    
    include "conn.php";
    $info = mysql_fetch_array(mysql_query("SELECT * FROM field6 WHERE varibleexample='$_COOKIE[variableexample]'"));
    if (!$info) { $info[id] = 0; }
    
    $error="";
    if($_POST[entry1]=="" || $_POST[entry2] == "" || $_POST[entry3] == ""){
    $contents = "All fields are required, hit your browser back button to correct the problem.";
     }
    else
    {
    //check if submission already exists
    
    $sql ="SELECT * FROM field WHERE entry1 = '$_POST[entry1]'";
    $rs = mysql_query($sql,$db) or die(mysql_error());
    
    if(mysql_numrows($rs) > 0){
    $contents = "Blah Blah Blah.";
          }
    else
    {
    //Save the data
    
    $entry4=trim($_POST[entry4]);
    $sq = "INSERT INTO field (entry1, entry2, entry3, entry4, entry5, entry6) VALUES ('$_POST[entry1]', '$_POST[entry2]', '$_POST[entry3]', '$entry4', '0', '$entry6')";
    @mysql_query($sq) or die(mysql_error());
    $contents = "<blah blah blah>";
    }
    }
    ?>
    PHP:
     
    yfs1, Aug 29, 2005 IP
  10. Perrow

    Perrow Well-Known Member

    Messages:
    1,306
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    140
    #10
    I'm not into PHP yet, otherwise I would code it out for you. Will start coding PHP any day now (my actual "space" site :D), hope someone else can solve it for you.

    Do you want to keep "in" and such all lower?
     
    Perrow, Aug 29, 2005 IP
  11. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Yes, I will need to include something similar to what you proposed as there is a short list of words that shouldn't be capitalized.

    Something tells me I will have to also have to code to change them down also.

    ie.

    Make
    The Brown Fox Ran In The Field to

    The Brown Fox Ran in the Field

    Then there is the fact that the first word needs to be capitalized even if its one of the no words.

    I think this is getting a bit too complicated for me as every time I think about it there are more scenarios :rolleyes:
     
    yfs1, Aug 29, 2005 IP
  12. BigTicket

    BigTicket Peon

    Messages:
    197
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Think this should do what you need. Lets use 'entry1' as the example field for this.


    <?php
    
    //Save the data
    
    $entry1 = ucwords(strtolower($_POST[entry1]));
    
    $entry4=trim($_POST[entry4]);
    $sq = "INSERT INTO field (entry1, entry2, entry3, entry4, entry5, entry6) VALUES ('entry1', '$_POST[entry2]', '$_POST[entry3]', '$entry4', '0', '$entry6')";
    @mysql_query($sq) or die(mysql_error());
    $contents = "<blah blah blah>";
    }
    }
    ?>
    PHP:

    In the above I added the first line of code, and in the the insert command you need to change the value from '$_POST[entry1]' to '$entry1' as I have.

    Again not sure which is actually your title field, just change accordingly.
     
    BigTicket, Aug 29, 2005 IP
  13. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #13
    Make your list of rules in English, in preferntial order (i.e. once one rule is hit for a word you don't follow any more) :
    e.g.
    First character is always a capital
    The words in, a, an, are, the, is and when are always lower case
    All words start with uppercase letter

    Writing the script to do it will be easy but defining the rules is the difficult part like what about abbreviations being uppercase, e.g. AM PM WTF

    This is one of those scripts that may turn into more hassle than it's actually worth.
     
    dct, Aug 29, 2005 IP
  14. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Thats true although there are rarely abbreviations and those can be fixed by hand.

    I will start where BigTicket indicated and try to write the rules from there.
     
    yfs1, Aug 29, 2005 IP
  15. BigTicket

    BigTicket Peon

    Messages:
    197
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Ah, my previous post does not account for that. As dct mentioned you would need to write the rules for all the words to be excluded.
     
    BigTicket, Aug 29, 2005 IP
  16. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #16
    It would probably only save me around 10-15 minutes a day so I am a little bit hesitant to get stuck in and spend a lot of time on it. Thanks for all the posts as I may still do it later.
     
    yfs1, Aug 29, 2005 IP
  17. Perrow

    Perrow Well-Known Member

    Messages:
    1,306
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    140
    #17
    That could easily be incorporated in my solution, just that the "if" checks if we're on the first word.

      if strWord exists in DontWords and not on first word then
    Code (markup):
    The fact that my PHP skills suck, suck :D I would have coded this up for you just because I like string handling functions so much (kind of a fetish actually :eek:).
     
    Perrow, Aug 29, 2005 IP