选择单选按钮时的AngularJs Form验证

时间:2018-11-23 10:08:13

标签: angularjs

嗨,我是angularJS的学习者,在我的表单中,我有姓名和电子邮件字段以及一个用于选择性别的广播组,而我的要求是当我选择男性时则需要姓名字段,如果我选择女性则则需要电子邮件字段< / p>

我尝试了下面的代码,但是它不能正常工作,请有人帮我

代码:

<form class="form-horizontal alert alert-warning" name="empList" id="empForm" ng-submit="insertInfo(empInfo)">

    <h3 class="text-center">Insert Employee Details Into Database</h3>

    <div class="form-group">
        <label for="Name">Employee Name:</label>
        <input type="text" name="emp_name" class="form-control" placeholder="Enter Employee Name" ng-model="empInfo.name"
            required="gender.value=='male'" />
    </div>

    <div class="form-group">
        <p class="text-danger" ng-show="empList.emp_name.$invalid">Name field is Empty!</p>
    </div>

    <div class="form-group">
        <label for="Email">Email Address:</label>
        <input type="email" name="emp_email" class="form-control" placeholder="Enter Employee Email Address" ng-model="empInfo.email"
            autofocus  required="gender.value=='female'"/>
    </div>
    <div class="form-group">
        <p class="text-danger" ng-show="empList.emp_email.$invalid>Invalid Email!</p>
    </div>

    <div class="form-group">
        <label for="Gender">Gender:</label>
        <label for="" class="radio-inline gender">
            <input type="radio" name="emp_gender" value="male" ng-model="empInfo.gender" #gender="ng-model">Male
        </label>
        <label for="" class="radio-inline gender">
            <input type="radio" name="emp_gender" value="female" ng-model="empInfo.gender" #gender="ng-model">Female
        </label>
    </div>

</form>

1 个答案:

答案 0 :(得分:0)

也许尝试进行验证: HTML:

      <form class="form-horizontal alert alert-warning" name="empList" id="empForm" ng-submit="insertInfo(empInfo)">

    <h3 class="text-center">Insert Employee Details Into Database</h3>

    <div class="form-group">
        <label for="Name" ng-show="empInfo.gender != male_value">Employee Name is required</label>
        <input type="text" name="emp_name" class="form-control" placeholder="Enter Employee Name" ng-model="empInfo.name"
            required="gender.value=='male'" />
    </div>

    <div class="form-group">
        <p class="text-danger" ng-show="empList.emp_name.$invalid">Name field is Empty!</p>
    </div>

    <div class="form-group">
        <label for="Email" ng-show="empInfo.gender == male_value">Email Address is rquired</label>
        <input type="email" name="emp_email" class="form-control" placeholder="Enter Employee Email Address" ng-model="empInfo.email"
              required="gender.value=='female'"/>
    </div>
    <div class="form-group">
        <p class="text-danger" ng-show="empList.emp_email.$invalid">Invalid Email!</p>
    </div>

    <div class="form-group">
        <label for="Gender">Gender:</label>
        <label for="" class="radio-inline gender">
            <input type="radio" name="emp_gender" ng-value="male_value" ng-model="empInfo.gender">Male
        </label>
        <label for="" class="radio-inline gender">
            <input type="radio" name="emp_gender" ng-value="female" ng-model="empInfo.gender" >Female
        </label>
    </div>

</form>

JS:

 $scope.male_value = 'male';

朋克:http://plnkr.co/edit/Hi5t1gU5TIdNx670wHo1?p=preview