Solved

Capitalisation javascript not woking

Posted on 2014-09-29
7
98 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 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

695 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