2015-10-27 18 views
0

我有一个疑问。我需要检查表单是否正确验证或不提交时使用angular.js.I已添加ng-message以验证该字段。我我在下面解释我的代码。如何使用angular.js提交表单时检查验证

<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> 

在这里,我可以得到适当的验证message.But这里我的要求是,当用户会点击提交按钮会如何现在检查的形式领域已经正确data.Please帮助我。

回答

1

如果您只是想知道表单是否有效,那么您可以轻松使用以下代码。您可以发送表单名称作为参数,然后在函数中添加以下代码。

$scope.adduserData = function(billdata){ 
     if(billdata.$valid){ 
     }else{ 
     } 
} 


    <input type="button" class="btn btn-success" ng-click="addUserData(billdata);"/>