AngularJS验证选择框

时间:2016-07-28 09:23:57

标签: javascript angularjs validation jquery-selectbox

在表单中有选择框,我尝试进行验证但不能像以前那样对输入类型文本做同样的工作。我做错了什么?

例如

<div class="form-group">
                                <label class="control-label col-sm-3">Email<sup>*</sup></label>

                                        <div class="col-sm-6">

                                            <div class="input-icon-right">
                                                <i class="fa fa-envelope"></i>
                                                <input class="form-control" placeholder="example@example.com" type="email" ng-model="patient.email" name="email" required>
                                                <p ng-show="myForm.email.$error.required" style="color:red" ng-if="myForm.email.$touched">Add your email</p>

                                            </div>

                                        </div>
                            </div>

此代码工作良好,并在字段为空时显示错误消息。

这个带有选择框的代码无法正常工作,每次选择没有空但有选择 - 选择 - 如何在没有其他选择时显示错误。

<div class="form-group">
                                <label class="col-sm-3 control-label">{{getWord('Gender')}}<sup>*</sup></label>
                                <div class="col-sm-6">
                                    <select ui-select2 ng-model="patient.gender"
                                            data-placeholder="Tên Cơ Sở"
                                            ng-change="changeGender()"
                                            style="width: 100%;" requied name="gender">
                                        <option value="">-{{getWord('Select')}}-</option>
                                        <option ng-repeat="item in genders" value="{{item.name}}">
                                            {{getWord(item.name)}}
                                        </option>
                                    </select>
                                    <p ng-show="myForm.gender.$error.required" style="color:red" ng-if="myForm.gender.$touched">Please choice gender</p>
                                </div>
                            </div>

3 个答案:

答案 0 :(得分:0)

<div class="form-group">
                            <label class="col-sm-3 control-label">{{getWord('Gender')}}<sup>*</sup></label>
                            <div class="col-sm-6">
                                <select ui-select2 ng-model="patient.gender"
                                        data-placeholder="Tên Cơ Sở"
                                        ng-change="changeGender()"
                                        style="width: 100%;" required name="gender">
                                    <option value="" disabled selected>-{{getWord('Select')}}-</option>
                                    <option ng-repeat="item in genders" value="{{item.name}}">
                                        {{getWord(item.name)}}
                                    </option>
                                </select>
                                <p style="color:red" ng-if="!myForm.gender.$pristine && myForm.gender.$error.required">Please choice gender</p>
                            </div>
                        </div>

你能试试吗?

答案 1 :(得分:0)

据我所知。当用户未选择任何内容或未触及下拉列表时,您需要显示错误。

更改此行:

<p ng-show="myForm.gender.$error.required" style="color:red" ng-if="myForm.gender.$touched">Please choice gender</p>

至此:

<p style="color:red" ng-if="myForm.gender.$pristine || myForm.gender.$error.required">Please choice gender</p>

还有一个拼写错误。将requied更改为required

纠正这个问题:

<select ui-select2 ng-model="patient.gender"
        data-placeholder="Tên Cơ Sở"
        ng-change="changeGender()"
        style="width: 100%;" requied 
name="gender">

至此:

<select ui-select2 ng-model="patient.gender"
        ng-change="changeGender()"
        style="width: 100%;" required 
name="gender">

希望这会有所帮助。

答案 2 :(得分:0)

我的最终代码。 但不是我想要的好

<div class="form-group" ng-class="{ 'has-error': myForm.gender.$pristine && myForm.gender.$invalid }">
                                <label class="col-sm-3 control-label">{{getWord('Gender')}}<sup>*</sup></label>
                                <div class="col-sm-6">
                                    <select ui-select2 ng-model="patient.gender" data-placeholder="Tên Cơ Sở" ng-change="changeGender(filterType)" style="width: 100%;" required name="gender">
                                        <option value="">-{{getWord('Select')}}-</option>
                                        <option ng-repeat="item in genders" value="{{item.name}}" >
                                            {{getWord(item.name)}}
                                        </option>
                                    </select>

                                     <p style="color:red" ng-if="myForm.gender.$pristine || myForm.gender.$error.required">Please choice gender</p>
                                </div>
                            </div>

screenshot