Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Upper and Lower Case in Text field

    Hi, I've the following code in my field

    Code:
    var pattern = /(\w)(\w*)/; // a letter, and then one, none or more letters 
    var a = document.forms[0].LName.value.split(/\s+/g); // split the sentence into an array of words
    
    for (i = 0 ; i < a.length ; i ++ ) {
    var parts = a[i].match(pattern); // just a temp variable to store the fragments in.
    
    var firstLetter = parts[1].toUpperCase();
    var restOfWord = parts[2].toLowerCase();
    
    a[i] = firstLetter + restOfWord; // re-assign it back to the array and move on
    }
    
    document.forms[0].LName.value = a.join(' '); 
    var f = document.forms[0];
    f.FullName.value = f.LName.value + " " + f.FName.value;
    whereby document.forms[0] is equivalent to this.form.

    It works, however, I'd like to modify it so that when users enter data in the field, not all capitals appear and not all lowercase letters.

    Eg:

    AT THE HOUSE would appear as

    At the house

    The above code would change it to

    At The House (which is not what I want)
    Last edited by Kylena; 09-29-2003 at 02:47 AM.

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This will Capitalize the first letter of a string. If you want to capitalize more than one input (sentence) you will have to split the array at periods(.) and loop thru for any other puncuation before you fire this script.

    <HTML>
    <HEAD>
    <SCRIPT type="text/javascript">
    <!--//
    function capitalizeFirst(form){
    s=form.t1.value;
    s=s.toLowerCase();

    /* Using a substring, we capitalize the first letter.
    If the first character is not alphabetical, then this will not work.
    Eg: using a number */
    s=s.substring(0,1).toUpperCase()+s.substring(1,s.length);
    form.t1.value=s;
    }
    //-->
    </SCRIPT>
    </HEAD>
    <BODY BGCOLOR="white">
    <FORM NAME="" METHOD="POST" ACTION="" ENCTYPE="TEXT/PLAIN">
    <INPUT TYPE="TEXT" NAME="t1" VALUE="AT THE HOUSE" SIZE="32">
    <br>
    <INPUT TYPE="BUTTON" VALUE="Capitalize first letter only" onClick="(capitalizeFirst(form));return false">
    </FORM>
    </BODY>
    </HTML>


    Hope this helps
    .....Willy
    Last edited by Willy Duitt; 09-29-2003 at 05:19 AM.

  • #3
    New Coder
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Willy. It works just like I wanted. I'd like to know how do I loop through the whole field like when the user has to type the following:

    I (will change to i, if this code is used.)
    periods (. the next letter should change to capital)

    I can't see how the loop should run.

  • #4
    New Coder
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'd like to modify the first code to include commas (,).

    How can I do that?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •