2017-10-05 7 views
2

私はAngularを初めて利用しています。私は角度4の反応形式を使用しており、カスタム検証の実行方法を理解しています。以下は私の数値の実装です日付と通貨の有効期限(4)

function numberValidator(c: AbstractControl): { [key: string]: boolean } | null { 
    if (c.pristine) { 
     return null; 
    } 
    if (c.value.match(/.*[^0-9].*/)) { 
     return { 'numeric': true }; 
    } 
    return null; 
} 

phoneControl: ['', [Validators.required, Validators.minLength(10), Validators.maxLength(10), numberValidator]], 

私は通貨(小数点以下2桁)または最も重要な日付フィールドを実行する方法を探しています。

これは他の場所で答えましたが、私は、角度のための任意のサンプルを見つけることができない場合(4)お時間を

おかげ

+0

:日付、このような何かのために? https://angular.io/api/common/CurrencyPipe – Wandrille

+0

提案していただきありがとうございます。私が間違っているなら、私を訂正してください。通貨パイプは、表示値を正しく変換するためのものですか?私はテキストボックスエントリの検証を探しています。 – Vinod

+0

myBad!あなたが正しいです! – Wandrille

答えて

4

は、日付の検証のどのような種類が必要ですか?私を許してちょうどその値が有効な日付ですか?入力要素にtype="date"を設定すると、ブラウザは有効な日付のみが入力されるようにします。

表示されている番号のバリデーターと通貨バリデーターの場合と同じです。 input要素にtype="number"を設定できるはずです。バリデーターは必要ありません。

まだの場合あなた自身の検証を実行したい場合は、例のように正規表現を使用できます。

通貨と日付の正規表現を検索するだけです。あなたがCurrencyPipeを使用していないのはなぜ形式MM/DD/YYYYを処理するためのカスタムバリデータを作成しJavascript - Regex to validate date format

+0

返事をありがとう。 type = "date"の設定は、すべてのブラウザまたはhtml5をサポートするブラウザでのみ機能しますか?より安全な側になるために、コンポーネント側で私自身の検証をしたいと思います。だからあなたは正規の検証のための正規表現を推奨している?ありがとう – Vinod

+0

これはあなたの例でここでやっていることです: '(c.value.match(/.*[^^ 0-9)。* /))'だから私はあなたが何か似たものを望んだと思った?そして、これを使用して特定のブラウザをサポートするブラウザを特定することができます:https://caniuse.com/#search=type%3D%22date%22 – DeborahK

+0

あなたの返信ありがとうございます。 – Vinod

1

とMMDDYYYY

function dateValidator(c: AbstractControl): { [key: string]: boolean } | null { 
    if (c.pristine) { 
     return null; 
    } 
    if ((c.value !== undefined && c.value !== '' && c.value != null)) { 

     var month = null; 
     var day = null; 
     var year = null; 
     var currentTaxYear = Number(new Date().getFullYear() - 1); 
     if (c.value.indexOf('/') > -1) { 
      var res = c.value.split("/");   
      if (res.length > 1) { 
       month = res[0]; 
       day = res[1] 
       year = res[2]; 
      }        
     } 
     else { 
      if (c.value.length == 8) { 
       month = c.value.substring(0, 2); 
       day = c.value.substring(2, 4); 
       year = c.value.substring(4, 8); 
      }    
     } 
     if (isNaN(month) || isNaN(day) || isNaN(year)) { 
      return { 'dateInvalid': true }; 
     } 
     month = Number(month); 
     day = Number(day); 
     year = Number(year); 
     if (month < 1 || month > 12) { // check month range 
      return { 'dateInvalid': true }; 
     } 
     if (day < 1 || day > 31) { 
      return { 'dateInvalid': true }; 
     } 
     if ((month === 4 || month === 6 || month === 9 || month === 11) && day === 31) { 
      return { 'dateInvalid': true }; 
     } 
     if (month == 2) { // check for february 29th 
      var isleap = (year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)); 
      if (day > 29 || (day === 29 && !isleap)) { 
       return { 'dateInvalid': true }; 
      } 
     } 
     if (year !== currentTaxYear) { 
      return { 'dateYearGreaterThanTaxYear': true }; 
     } 
    } 
    return null; 
} 
関連する問題