Solved

Capitalisation javascript not woking

Posted on 2014-09-29
7
94 Views
Last Modified: 2014-09-30
I am using the following code to try to capitalise text in input boxes.

str = str.replace(/[^-'\s]+/g, function(word) {
  return word.replace(/^./, function(first) {
    return first.toUpperCase();
  });
});

function cnvrt2Upper(str) {
        return str.toLowerCase().replace(/\b[a-z]/g, cnvrt);
        function cnvrt() {
            return arguments[0].toUpperCase();
        }
    }

In Firefox the first function reports that .replace is not a valid function, and when I run the second function it says .toLowerCase is not a function.

What am I doing wrong?
0
Comment
Question by:jdthedj
  • 4
  • 3
7 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 40351419
the following work for me : http://jsfiddle.net/u93mhq6p/
function cnvrt2Upper(str) {
    return str.charAt(0).toUpperCase() + str.toLowerCase().substr(1);
}

Open in new window

0
 
LVL 3

Author Comment

by:jdthedj
ID: 40351423
Thanks leakim971, but I need to capitalise each word id there's more than one word in a text box
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40351431
updated : http://jsfiddle.net/u93mhq6p/1/
function cnvrt2Upper(str) {
    var cap = function(s) {
        return s.charAt(0).toUpperCase() + s.toLowerCase().substr(1);
    };
    return str.replace(/([^-'\s]+)/g, cap);
}

alert( cnvrt2Upper("hello mister how are you") );

Open in new window

0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 3

Author Comment

by:jdthedj
ID: 40351436
I still get the following error in Firefox


TypeError: str.replace is not a function
      

...unction(s) { return s.charAt(0).toUpperCase() + s.toLowerCase().substr(1); }; re..
0
 
LVL 3

Author Comment

by:jdthedj
ID: 40351439
I have done some more experimenting - your example works with the alert, but not if on the form I have this

<input type=text size=40 onBlur="cnvrt2Upper(this)">
0
 
LVL 3

Author Closing Comment

by:jdthedj
ID: 40351467
Finally got it working - thanks leakim971!
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40351880
<input type=text size=40 onBlur="cnvrt2Upper(this.value)">

Open in new window

0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question