Solved

Capitalisation javascript not woking

Posted on 2014-09-29
7
91 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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'…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now