Solved

Capitalisation javascript not woking

Posted on 2014-09-29
7
93 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

911 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

25 Experts available now in Live!

Get 1:1 Help Now