用于检查唯一电子邮件ID的自定义指令问题

时间:2016-06-07 07:29:44

标签: angularjs asp.net-mvc angularjs-directive

我正在开发Angularjs表单,我需要验证输入值是否在数据库中。 我的html如下

[button setBackgroundColor:[UIColor greenColor]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

指令如下

<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 });
            };
        }
    }
}])

我现在遇到的问题是。

1。电子邮件ID作为[HttpGet] public ActionResult CheckExternalUserExists(string emailid) { if (AccountExecutor.UserExists(emailid)) { return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Exists"); } return new HttpStatusCodeResult(HttpStatusCode.OK, "Not Exists"); } 传递给控制器​​。

2。我想显示两条消息

a)如果电子邮件ID是唯一的,那么消息应该说明其独特的内容

b)如果电子邮件ID存在,那么它应该显示它存在的消息,    当前消息未显示仅显示红色边框。

1 个答案:

答案 0 :(得分: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]