2017-02-22 6 views
1

私のテキストエリアの最初の文字と3番目の文字を大文字にしたい。最初と3番目の文字をどのように大文字にするのですか?

最初の文字のみを大文字にして、次のすべての文字と単語を小文字に変換します。

この問題の解決策がある場合は、教えてください。

私はAngularJSを使用しています。

これは私が試してやったことです。

link: function (scope, iElement, iAttrs, controller) { 
     //console.log('init'); 

     controller.$parsers.push(function (inputValue) { 
      var transformedInput = (!!inputValue) ? inputValue.charAt(0).toUpperCase() + inputValue.substr(1).toLowerCase() : ''; 
         if (transformedInput != inputValue) { 
      controller.$setViewValue(transformedInput); 
      controller.$render(); 
     } 

     return transformedInput; 
    }); 

これは最初の文字に対してのみ機能し、大文字に変換して別の文字と単語を小文字に変換します。

私はこのコードに変更を加えようとしましたが、何も変更しませんでした。ここで

var transformedInput = (!!inputValue) ? inputValue.charAt(0).toUpperCase() + inputValue.substr(1).toLowerCase() + inputValue.charAt(3).toUpperCase() + inputValue.substr(4).toLowerCase(): ''; 

答えて

2

これをご覧ください。 forループを使って変更する文字インデックスを特定するのと同じです。

var inputValue = "test"; 
 

 
var transformedInput = ''; 
 

 
if(inputValue){ 
 
    for(var i=0; i<inputValue.length; i++){ 
 
    \t if(i===0 || i=== 2){ 
 
\t  transformedInput += inputValue.charAt(i).toUpperCase(); 
 
     } else { 
 
    \t  transformedInput += inputValue.charAt(i).toLowerCase(); 
 
    } 
 
    } 
 
} 
 

 
console.log(transformedInput);

+0

ありがとう、それはうまく動作します!どうもありがとう – brithwulf

1

次のようにファイル名を指定して実行

function capitalizeAtPositions(string, indexes) { 
    (indexes || []).forEach(function(index) { 
    if (string.length < index) return; 

    string = string.slice(0, index) + 
     string.charAt(index).toUpperCase() + string.slice(index+1); 
    }); 
    return string; 
} 

特定の位置に文字を大文字にする機能です。

var test = "abcdefg"; 
var result = capitalizeAtPositions(test, [0, 2]); 
//AbCdefg 

をあなたのケースでは、私はそれが何か(できないようになると思いますjsfiddleなしでテストしてください):

var transformedInput = capitalizeAtPositions(inputValue || '', [0, 2]); 
+0

することができます私はちょうどこのコードを私のものに置き換えますか?テキストではなくバージョンを入力する必要があります。私は何かを入力するときに最初の文字を大文字にする必要があります。例えば3番目の文字です。私はこれがテキストの入力ではないと思う。 – brithwulf

+0

@brithwulf yep、そうすることができる。入力時にテキストを変更する必要がある場合は、簡単に変更することはできません。あなたは 'div contenteditable'または' CodeMirror'のようなもっと高度な解決策を使うべきでしょう、と私は思います。 – euvl

0

お試しください

var firstUpperCase = inputValue.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); 

    var transformedInput = ''; 
     for(i = 0; i < firstUpperCase.length; i++){ 
      if(i > 0 && i % 3 == 0){ 
       transformedInput += firstUpperCase[i].toUpperCase(); 
      }else{ 
       transformedInput += firstUpperCase[i]; 
      } 
     } 
1

あなたはおそらくディレクティブが必要になるでしょう、あなたが入力すると、入力変化を持っている必要がありますどのように見て、ここでのNG-モデルと任意の入力の特定の文字を大文字にする一つです:

https://plnkr.co/edit/hWhmjQWdrghvsL20l3DE?p=preview

app.directive('myUppercase', function() { 
    return { 
    scope: { 
     positions: '=myUppercase' 
    }, 
    require: 'ngModel', 
    link: function(scope, elem, attrs, ngModelCtrl) { 

     scope.positions = scope.positions || [] 

     function makeString(string) { 
     if (!string) return; 
     angular.forEach(scope.positions, function(pos) { 
      string = string.slice(0, pos) + string.slice(pos, pos+1).toUpperCase() + string.slice(pos + 1) 
      console.log(string) 
     }) 
     return string; 
     } 

     ngModelCtrl.$parsers.push(makeString) 
     ngModelCtrl.$formatters.push(makeString) 
    } 
    } 
}) 

HTML:

<input ng-model="value" my-uppercase="[0, 2]"> 
1

私のシンプルなソリューション

var inputValue = 'your value'; 

function toUpper (str) { 
    var result = ''; 
    for (var i = 0; i < str.length; i++) { 
     if (i === 0 || i === 2) { 
      result += str[i].toUpperCase(); 
     } else { 
      result += str[i].toLowerCase(); 
     } 
    } 
    return result; 
} 
var transformedInput = toUpper(inputValue); 
関連する問題