-1

i have a multiline textfield("Adressfeld"), and i want to Uppercase every first letter and LowerCase the rest of every single word in this text area.

Here is my try:

function capitalize(Eingabe){
Eingabe = this.getField("Adressfeld").value;
var strArr = Eingabe.split(" ");
var newArr = [];

for(var i = 0 ; i < strArr.length ; i++ ){

var FirstLetter = strArr[i].charAt(0).toUpperCase();
var restOfWord = strArr[i].slice(1).toLowerCAse();

newArr[i] = FirstLetter + restOfWord;

}

return newArr.join(' ');

} 

Ausgabe = this.getField("Empfängername");
Ausgabe.value = capitalize();

With the script shown above, every single word in the first line of the text area is capitalized. But in every other line, the first word isn't capitalized. How i have to change the script to get it work? Thanks,

renokl2014
  • 121
  • 1
  • 11

6 Answers6

0

Meinst du sowas?

var textToArray = document.getElementById('myTextarea').value.split('\n');
/* Hier eine Schleife zur Bearbeitung von textToArray */
var arrayToString = textToArray.join(' ');
Entimon
  • 184
  • 3
  • 11
0

The split operation fails -- the result of the first split cannot be split again on another character.

I see no reason to first replace returns by a single type \n and then try to split on either \n or a space. It's way easier to just replace the returns by a single space and only then split:

var strArr = String(Eingabe.value).replace(/[\r\n]+/g," ").split(" ");

With that, the rest seems to work.


Here is another approach, which may or may not work as well (it depends on whether Javascript's interpretation of "word boundary" \b agrees with your own):

function capitalize(Eingabe){
// Eingabe = this.getField("Adressfeld");
    var strArr = String(Eingabe.value).replace(/[\r\n ]+/g,' ');
    strArr = strArr.replace (/\b[a-z]/g, function(found) { return found.toUpperCase(); });
    return strArr;
}
Jongware
  • 22,200
  • 8
  • 54
  • 100
0

A few things:

• The argument of the function is Eingabe. In this case, this is a variable containing a value, and it does not make sense at all to redefine in the first line of the function. The way the function looks like, you won't need an argument, and therefore, your function definition looks like this:

function capitalize() {

• That done, define Eingabe properly as var Eingabe .

• With the array, you essentially want to create a two-dimensional array. First create the array of lines and then a loop through the lines, as it has been suggested in the answer by @Entimon

• You end with

this.getField("Adressfeld").value = capitalize() ;

And that should do it.

Max Wyss
  • 3,549
  • 2
  • 20
  • 26
0

thanks for your help! The correct answer is based on Arunprasanth KV's jsfiddle.net/svh1jd99

function capitalize()

{
var capitalize = this.getField("Adressfeld").value;
var done = capitalize.replace(/\b./g, function(m){ return m.toUpperCase();});
return done;
};

this.getField("Adressfeld").value = capitalize();

Thanks again for your help.

renokl2014
  • 121
  • 1
  • 11
0

I have included an example below try like that, it will solve your problem

Html

<input type="button" value="clk" onclick="z();"/>
<textarea rows="4" id="text" cols="50">

JS

function z()
{

var z=document.getElementById("text").value;
var x=z.replace(/\b./g, function(m){ return m.toUpperCase(); });
alert(x);
}

DEMO

I you want to Convert Every first letter of each word to upper and all other letters are lower then first convert the entire string to lowercase.Then do the same things as above.

DEMO2

Arunprasanth K V
  • 20,733
  • 8
  • 41
  • 71
  • thanks again. But i was a little bit to fast, saying that this code works exactly the way i want it to. How this code has to look like if i want to UpperCase every first letter of every word, and LowerCase the rest? – renokl2014 Feb 20 '15 at 00:01
  • @renokl2014 In your question you did not mention that , that is why i had you this solution, anyway if you want to do something like that then first covert the entire string to lowercase then do the same what we are done first, i had updated the answer with working demo you can refer that . – Arunprasanth K V Feb 20 '15 at 04:31
0

By using jQuery you can do this as shown below:

Demo: https://jsfiddle.net/cxow8198/3/

<input type="text" id="input">

<script>
//usage
$("input").keyup(function() {
   toUpper(this);
});

//function
function toUpper(obj) {
    var mystring = obj.value;
    var sp = mystring.split(' ');
    var wl=0;
    var f ,r;
    var word = new Array();
    for (i = 0 ; i < sp.length ; i ++ ) {
        f = sp[i].substring(0,1).toUpperCase();
        r = sp[i].substring(1).toLowerCase();
        word[i] = f+r;
    }
    newstring = word.join(' ');
    obj.value = newstring;
    return true;   
}
</script>

Query code snippet to make capitals of the first letter of every word in the string. This could be used to prevent users from entering all caps for titles or text when they input data into forms.

Murat Yıldız
  • 11,299
  • 6
  • 63
  • 63