Angularjs 如何对angular中的必填字段进行表单验证
我想通过使用角度表单验证来验证我的表单。我已经阅读了许多文章,这些文章提供了如何在提交之前验证表单的信息。但我的问题是如何验证必填字段的表单 e、 对于我的登录表单,我有两个字段,一个是用户名和密码。在不输入任何字段值的情况下,用户单击登录按钮。所以在这个场景中,如何在angular中进行验证Angularjs 如何对angular中的必填字段进行表单验证,angularjs,Angularjs,我想通过使用角度表单验证来验证我的表单。我已经阅读了许多文章,这些文章提供了如何在提交之前验证表单的信息。但我的问题是如何验证必填字段的表单 e、 对于我的登录表单,我有两个字段,一个是用户名和密码。在不输入任何字段值的情况下,用户单击登录按钮。所以在这个场景中,如何在angular中进行验证 提前感谢,只需在提交按钮上添加一个ng disabled。类似的东西应该可以使用。您必须为表单指定一个名称,以便进行角度验证 <form name="myform"> <input
提前感谢,只需在提交按钮上添加一个ng disabled。类似的东西应该可以使用。您必须为表单指定一个名称,以便进行角度验证
<form name="myform">
<input type="email" name="email" ng-model="email" placeholder="Email Address" required /></input>
<input type="password" name="password" required></input>
<button type="submit" ng-disabled="myform.$invalid"> Submit </button>
</form>
提交
只需在“提交”按钮上添加禁用的ng即可。类似的东西应该可以使用。您必须为表单指定一个名称,以便进行角度验证
<form name="myform">
<input type="email" name="email" ng-model="email" placeholder="Email Address" required /></input>
<input type="password" name="password" required></input>
<button type="submit" ng-disabled="myform.$invalid"> Submit </button>
</form>
提交
只需在“提交”按钮上添加禁用的ng即可。类似的东西应该可以使用。您必须为表单指定一个名称,以便进行角度验证
<form name="myform">
<input type="email" name="email" ng-model="email" placeholder="Email Address" required /></input>
<input type="password" name="password" required></input>
<button type="submit" ng-disabled="myform.$invalid"> Submit </button>
</form>
提交
只需在“提交”按钮上添加禁用的ng即可。类似的东西应该可以使用。您必须为表单指定一个名称,以便进行角度验证
<form name="myform">
<input type="email" name="email" ng-model="email" placeholder="Email Address" required /></input>
<input type="password" name="password" required></input>
<button type="submit" ng-disabled="myform.$invalid"> Submit </button>
</form>
提交
以下是验证表单的代码(根据我的要求使用电子邮件内置验证器)
Login.Component.html
<form id="loginForm" #validform="ngForm" (ngSubmit)="onSubmit(validform)">
<div class="example-container">
<mat-form-field>
<input matInput placeholder="UserName" [formControl]="userName" autofocus>
<mat-error *ngIf="userName.hasError('email') && !userName.hasError('required')">
Please enter a valid email address
</mat-error>
<mat-error *ngIf="userName.hasError('required')">
Email is
<strong>required</strong>
</mat-error>
<br>
</mat-form-field>
<mat-form-field>
<input matInput type="password" placeholder="Password" [formControl]="password">
<mat-error *ngIf="password.hasError('required')">
Password is
<strong>required</strong>
</mat-error>
</mat-form-field>
</div>
<button type="submit" class="btn btn-success btn-block" [disabled]="userName.hasError('email') || password.hasError('required')"
id="login_btn">Login</button>
</form>
希望这会有所帮助。以下是验证表单的代码(根据我的要求使用电子邮件内置验证程序) Login.Component.html
<form id="loginForm" #validform="ngForm" (ngSubmit)="onSubmit(validform)">
<div class="example-container">
<mat-form-field>
<input matInput placeholder="UserName" [formControl]="userName" autofocus>
<mat-error *ngIf="userName.hasError('email') && !userName.hasError('required')">
Please enter a valid email address
</mat-error>
<mat-error *ngIf="userName.hasError('required')">
Email is
<strong>required</strong>
</mat-error>
<br>
</mat-form-field>
<mat-form-field>
<input matInput type="password" placeholder="Password" [formControl]="password">
<mat-error *ngIf="password.hasError('required')">
Password is
<strong>required</strong>
</mat-error>
</mat-form-field>
</div>
<button type="submit" class="btn btn-success btn-block" [disabled]="userName.hasError('email') || password.hasError('required')"
id="login_btn">Login</button>
</form>
希望这会有所帮助。以下是验证表单的代码(根据我的要求使用电子邮件内置验证程序) Login.Component.html
<form id="loginForm" #validform="ngForm" (ngSubmit)="onSubmit(validform)">
<div class="example-container">
<mat-form-field>
<input matInput placeholder="UserName" [formControl]="userName" autofocus>
<mat-error *ngIf="userName.hasError('email') && !userName.hasError('required')">
Please enter a valid email address
</mat-error>
<mat-error *ngIf="userName.hasError('required')">
Email is
<strong>required</strong>
</mat-error>
<br>
</mat-form-field>
<mat-form-field>
<input matInput type="password" placeholder="Password" [formControl]="password">
<mat-error *ngIf="password.hasError('required')">
Password is
<strong>required</strong>
</mat-error>
</mat-form-field>
</div>
<button type="submit" class="btn btn-success btn-block" [disabled]="userName.hasError('email') || password.hasError('required')"
id="login_btn">Login</button>
</form>
希望这会有所帮助。以下是验证表单的代码(根据我的要求使用电子邮件内置验证程序) Login.Component.html
<form id="loginForm" #validform="ngForm" (ngSubmit)="onSubmit(validform)">
<div class="example-container">
<mat-form-field>
<input matInput placeholder="UserName" [formControl]="userName" autofocus>
<mat-error *ngIf="userName.hasError('email') && !userName.hasError('required')">
Please enter a valid email address
</mat-error>
<mat-error *ngIf="userName.hasError('required')">
Email is
<strong>required</strong>
</mat-error>
<br>
</mat-form-field>
<mat-form-field>
<input matInput type="password" placeholder="Password" [formControl]="password">
<mat-error *ngIf="password.hasError('required')">
Password is
<strong>required</strong>
</mat-error>
</mat-form-field>
</div>
<button type="submit" class="btn btn-success btn-block" [disabled]="userName.hasError('email') || password.hasError('required')"
id="login_btn">Login</button>
</form>
希望这会有所帮助。为什么不直接使用HTMLW的
required
属性呢?为什么不直接使用HTMLW的required
属性呢?为什么不直接使用HTMLW的required
属性呢HTML@dip似乎是正确答案,也许奖励正确答案?@dip似乎是正确答案,也许奖励正确答案?@dip似乎是正确答案,也许奖励正确答案?@dip似乎是正确答案,也许奖励正确答案?