【问题标题】:How to validate date in angular 4如何在角度 4 中验证日期
【发布时间】:2018-06-29 14:46:15
【问题描述】:

我是 angular 新手,在我的应用程序中,我需要验证日期并根据验证显示跨度消息。

这里我使用的是 ngx-bootstrap datepicker。

HTML:

<label for="mDOB" class="control-label">Date of birth</label>
            <input [bsConfig]="bsConfig"  type="text" maxlength="10" name="mDOB" [(ngModel)]="mUser.mDOB" #mDOB="ngModel"
              placeholder="Date of birth" class="form-control" bsDatepicker required>
            <div class="help-block alert-danger col-sm-12" *ngIf="mDOB.errors?.required && mDOB.touched">
              * Date of Birth is required
            </div>

在这里我已经完成了所需的字段验证。它工作正常

现在我需要验证日期字段仅接受数字,格式应为“mm/dd/yyyy”。

app.component.ts

submitForm(newUser: User): void {

    //  pattern="^(0?[1-9]|1[0-2])/(0?[1-9]|1[0-9]|2[0-9]|3[01])/\d{4}$";
    // newUser.mDOB using this I can get the selected date .
    // it always returns 2018-06-11T18:30:00.000Z in this format 
    // So here I need to convert from this format to "mm/dd/yyyy"
  }

谁能帮我解决这个问题。

【问题讨论】:

标签: angular typescript


【解决方案1】:

您想要什么类型的日期验证?只是价格是有效日期?如果您在 enter 元素中设置了 sort="date",浏览器将确保只输入合法的日期。

与您显示的范围验证器和任何货币验证器相同。您需要能够在输入详细信息上设置 type="wide Variation",并且您可能不需要验证器。

如果您仍想执行您自己的验证,您可以使用普通表达式,就像您的实例一样。

只显示货币和日期的正则表达式。对于日期,类似这样:javascript - regex to validate date layout

【讨论】:

猜你喜欢
  • 2020-11-18
  • 1970-01-01
  • 2016-10-04
  • 2018-05-01
  • 2021-02-13
  • 2020-07-13
  • 2020-09-20
  • 1970-01-01
  • 2019-03-04
相关资源
最近更新 更多