我想避免在我的角2形式的空白/空格? 這可能嗎? 這怎麼辦?如何驗證空白處/空白處? [Angular 2]
4
A
回答
6
也許這篇文章可以幫助你http://blog.angular-university.io/introduction-to-angular-2-forms-template-driven-vs-model-driven/
在這種方法中,你必須使用FormControl然後查看值的變化,然後你的面膜適用於價值。一個例子應該是:
...
form: FormGroup;
...
ngOnInit(){
this.form.valueChanges
.map((value) => {
// Here you can manipulate your value
value.firstName = value.firstName.trim();
return value;
})
.filter((value) => this.form.valid)
.subscribe((value) => {
console.log("Model Driven Form valid value: vm = ",JSON.stringify(value));
});
}
2
爲了避免表單子索引,只需在輸入字段中使用required
attr。
<input type="text" required>
或者,提交後
當窗體submited,你可以使用str.trim()刪除空格形成開始和字符串的結尾。我做了一個提交功能,向您展示:
submitFunction(formData){
if(!formData.foo){
// launch an alert to say the user the field cannot be empty
return false;
}
else
{
formData.foo = formData.foo.trim(); // removes white
// do your logic here
return true;
}
}
22
您可以創建一個自定義驗證器來處理這個問題。
new FormControl(field.fieldValue || '', [Validators.required, this.noWhitespaceValidator])
添加noWhitespaceValidator方法您的組件
public noWhitespaceValidator(control: FormControl) {
let isWhitespace = (control.value || '').trim().length === 0;
let isValid = !isWhitespace;
return isValid ? null : { 'whitespace': true }
}
,並在HTML
<div *ngIf="yourForm.hasError('whitespace')">Please enter valid data</div>
2
我所做的就是創建一個驗證器,除了我添加了該做的samething的角度進行的minLength修剪()
import { Injectable } from '@angular/core';
import { AbstractControl, ValidatorFn, Validators } from '@angular/forms';
@Injectable()
export class ValidatorHelper {
///This is the guts of Angulars minLength, added a trim for the validation
static minLength(minLength: number): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } => {
if (ValidatorHelper.isPresent(Validators.required(control))) {
return null;
}
const v: string = control.value ? control.value : '';
return v.trim().length < minLength ?
{ 'minlength': { 'requiredLength': minLength, 'actualLength': v.trim().length } } :
null;
};
}
static isPresent(obj: any): boolean {
return obj !== undefined && obj !== null;
}
}
然後我在我的app.component.ts推翻由角
import { Component, OnInit } from '@angular/core';
import { ValidatorHelper } from 'app/common/components/validators/validator-helper';
import { Validators } from '@angular/forms';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent implements OnInit {
constructor() { }
ngOnInit(): void {
Validators.minLength = ValidatorHelper.minLength;
}
}
提供的minLength功能現在到處角的的minLength內置驗證器會使用您的助手創建的minLength。
Validators.compose([
Validators.minLength(2)
]);
相關問題
- 1. 如何驗證空白?
- 2. scanf如何處理空白空間?
- 3. ANTLR4:空白處理
- 4. HtmlUnit處理空白
- 5. Atom/vi - 處理空白的空白處理
- 6. w3c驗證目標空白
- 7. 空白驗證工作不
- 8. 底部html5空白處jquery
- 9. 絲帶菜單空白處
- 10. 處理SVN中的空白?
- 11. UITabBarController頂部的空白處
- 12. 底部的空白處
- 13. Bootstrap Popover - 頂部空白處
- 14. StringLengthAttribute MVC空白處末尾
- 15. 如何處理$ _GET中的空白?
- 16. ANTLR4:空白和空間詞彙處理
- 17. 剝離尾隨空白處的好處?
- 18. APEX 3.2針對空白和空白的文本驗證
- 19. 角2 + MDL空白頁
- 20. BRE如何匹配行結束處的空白空間?
- 21. 如何處理不是空的,不能修剪的「空白」?
- 22. Windows批處理:如何刪除所有空白(或空)行
- 23. 電子郵件中的HTML空白處
- 24. iframe中頂部的空白處?
- 25. awk處理含有空白的變量
- 26. 空白的空白,在Firefox
- 27. 刪除頁腳下的小空白處
- 28. 批處理文件空白變量
- 29. 如何爲空或空白
- 30. 處理servlet時獲取空白屏幕
你只需要修剪字段作爲雙向數據綁定。你也可以考慮自定義管道 – afmeva
也許這篇文章可以幫助你http://blog.angular-university.io/introduction-to-angular-2-forms-template-driven-vs-model-driven/ – vinagreti