[英]How to check validation while submitting form using angular.js
我有一个疑问。我需要使用angular.js在提交时检查表单是否正确验证。我添加了ng-message
来验证字段。我在下面解释我的代码。
<form name="billdata" id="billdata" enctype="multipart/form-data" novalidate>
<div class="input-group bmargindiv1 col-md-12">
<span class="input-group-addon ndrftextwidth text-right" style="width:180px">Email Id :</span>
<input type="email" name="email" id="contactno" class="form-control" placeholder="User email" ng-model="user_email" >
</div>
<div class="help-block" ng-messages="billdata.email.$error" ng-if="billdata.email.$touched">
<p ng-message="email" style="color:#F00;">This needs to be a valid email</p>
</div>
<div class="input-group bmargindiv1 col-md-12">
<span class="input-group-addon ndrftextwidth text-right" style="width:180px">Mobile No :</span>
<input type="text" name="mobno" id="contactno" class="form-control" placeholder="Mobile No" ng-model="mob_no" ng-minlength="10" ng-maxlength="10" >
</div>
<div class="help-block" ng-messages="billdata.mobno.$error" ng-if="billdata.mobno.$touched">
<p ng-message="minlength" style="color:#F00;">This field is too short.The min length of your mob no should be 10.</p>
<p ng-message="maxlength" style="color:#F00;">This field is too long.The max length of your mob no should be 10.</p>
</div>
</div>
<div class="clearfix"></div>
<div style="text-align:center; padding-top:10px;">
<input type="button" class="btn btn-success" ng-click="addUserData();" id="addProfileData" ng-value="buttonName" />
<!-- <button class="btn btn-success" type="button" ng-click="addCourseData();">Submit</button>-->
</div>
</form>
在这里我可以获得正确的验证消息。但是在这里,我的要求是当用户单击“提交”按钮时,它将如何检查表单字段中的数据是否正确。请帮助我。
如果只想知道表单是否有效,只需使用以下代码即可。 您可以将表单名称作为参数发送,然后在函数中添加以下代码。
$scope.adduserData = function(billdata){
if(billdata.$valid){
}else{
}
}
<input type="button" class="btn btn-success" ng-click="addUserData(billdata);"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.