2016-06-07 26 views
1

我正在Angularjs表单上,我需要验证输入值是否在数据库上。 我的HTML是如下问题与自定义指令检查唯一的电子邮件ID

<div class="form-group mg-btm0" show-errors> 
       <label class="control-label col-sm-4 fw-norm txt-right">Email ID:</label> 
       <div class="col-sm-8"> 
        <input type="email" class="form-control" placeholder="Email ID" name="emailAddress" ng-required="true" autocomplete="Off" ng-model="User.emailId" 
         ng-maxlength="100" emailid-available-validator> 
        <span class="help-block" ng-if="RegisterUser.emailAddress.$error.email">A valid email is required.</span> 
        <span class="help-block" ng-if="RegisterUser.emailAddress.$error.maxlength">Max length is 100.</span> 
        <span class="help-block" ng-if="RegisterUser.emailAddress.$pending"> Checking emailId...</span> 
        <span class="help-block" ng-if="RegisterUser.emailAddress.emailidAvailable">EmailId entered has already been registered</span> 
       </div> 
      </div> 

指令是如下

angularFormsApp.directive('emailidAvailableValidator', ['$http', function ($http) { 
    var baseurl = window.location.protocol + "//" + window.location.host + "/"; 
    return { 
     require: 'ngModel', 
     link: function ($scope, element, attrs, ngModel) { 
      ngModel.$asyncValidators.emailidAvailable = function (value) { 
       //var emailid = { 'emailid': emailaddress }; 
       return $http.get(baseurl + "Users/CheckExternalUserExists/", { emailid: value }); 
      }; 
     } 
    } 
}]) 

和控制器如下

[HttpGet] 
     public ActionResult CheckExternalUserExists(string emailid) 
     { 
      if (AccountExecutor.UserExists(emailid)) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Exists"); 
      } 
      return new HttpStatusCodeResult(HttpStatusCode.OK, "Not Exists"); 
     } 

现在我有问题的。

1。电子邮件ID正在作为null传递给控制器​​。

2。我想说明两个消息

a)如果电子邮件ID是唯一的,那么消息应该说,它的一些独特的

b)在随后的电子邮件ID存在它应该显示它的存在消息, 目前的消息是不是被只显示红色边框。

回答

0
try below in your code 

    ngModel.$asyncValidators.emailidAvailable = function (ngModel.$modelValue) { 
      //var emailid = { 'emailid': emailaddress }; 
      return $http.get(baseurl + "Users/CheckExternalUserExists/", { emailid: value }); 
     }; 

    check out below link for more information... 
    [asyncValidators][1] 
+0

它有什么不同? – Mahajan344