gpt4 book ai didi

Angular6 - 如何正确验证和不接受空输入字段

转载 作者:搜寻专家 更新时间:2023-10-30 21:56:41 26 4
gpt4 key购买 nike

我正在使用 Angular 并试图弄清楚如何正确验证输入字段并且它不应该接受空格。我尝试使用如下所示的 IF 语句 对其进行试验。它似乎工作正常,但当一个输入字段为空白时,我收到此错误消息:

ERROR TypeError: formValue.employee.trim is not a function

onSubmit() { //when  the user clicks the submit button
const formValue = this.telephoneForm.value;
this.submitted = true;
if (this.telephoneForm.invalid) {
const invalidControls = this._findInvalidControls();
if (invalidControls.length) {
$("[formControlName=" + invalidControls[0] + "]").focus();
}
}

if (
this.telephoneForm.invalid ||
this.alreadyExist ||
this.noEmployeesFound ||
this.noDepartmentsFound ||
this.noVendorsFound
) {
return;
}


if (
(formValue["department"] === null ||
formValue["department"].trim() === "") ||
formValue["employee"] === null ||
formValue["employee"].trim() === "" ||
formValue["vendor"] === null ||
formValue["vendor"].trim() === ""
) {
$("[formControlName='department']").focus();
$("[formControlName='employee']").focus();
$("[formControlName='vendor']").focus();

this.noEmployeesFound = true;
this.noDepartmentsFound = true;
this.noVendorsFound = true;

return false;
}
}

看这个 screenshot of the form where I use this code

最佳答案

试试这个。

formValue.employee.toString().trim()

关于Angular6 - 如何正确验证和不接受空输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54140755/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com