?
Solved

Capitalisation javascript not woking

Posted on 2014-09-29
7
Medium Priority
?
101 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…
Suggested Courses

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