2015-06-22 105 views
1

选择下拉菜单并单选按钮验证不起作用。请找到下面的代码。 HTML required选项已经存在于标签中,但仍然不起作用。我错过了什么?在angularjs中选择选项和单选按钮验证

这里是plunker

<!DOCTYPE html> 
<html ng-app="myApp" > 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"> 
     <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> 
     <script> 
      var app = angular.module('myApp', []); 

      app.controller('MainCtrl', function ($scope) { 
       $scope.cityArray = ["hyderabad", "secunderabad", "delhi", "mumbai"]; 
       $scope.submit = function ($event) { 
        if ($scope.myForm.$invalid) { 
         // Or some other update 
         $scope.myForm.$submitted = true; 
         $event.preventDefault(); 
        } 
       } 
      }); 
     </script> 

     <title>Registration Form</title> 
    </head> 
    <body ng-controller="MainCtrl"> 
     <div class="container"> 
      <div class="col-md-6 col-md-offset-3"> 
       <div class="panel panel-login"> 
        <div class="panel-body"> 
         <div class="row"> 
          <div class="col-lg-12"> 
           <h2 class="text-muted">Registration form</h2> 
           <div> 
            <form name="myForm" action="RegistrationServlet.do" method="POST" > 
             First name:<input type="text" class="form-control input-sm" name="uname" ng-pattern="/^[a-zA-Z]{3,20}/" ng-model="uname" placeholder="First Name" required/> 
             <span style="color:red" ng-show="myForm.uname.$error.pattern">First name cannot be less than 3 letters with no digits</span> 
             <span style="color:red" class="error" ng-if="myForm.$submitted && myForm.uname.$error.required">Please fill field above<br></span><br/> 

             Last name:<input type="text" class="form-control input-sm" name="lname" ng-model="lname" ng-pattern="/^[a-zA-Z]{3,20}/" required placeholder="Last Name"/> 
             <span style="color:red" ng-show="myForm.lname.$error.pattern">Last name cannot be less than 3 letters with no digits</span><br/> 
             <span style="color:red" class="error" ng-if="myForm.$submitted && myForm.lname.$error.required">Please fill field above<br></span> 

              Password: 
              <input type="password" class="form-control input-sm glyphicon glyphicon-ok" name="pwd" ng-minlength="3" ng-model="pwd" required placeholder="Password"/> 
              <span style="color:red" ng-show="myForm.pwd.$error.minlength">password cannot be less than 3 letters</span><br/> 
              <span style="color:red" class="error" ng-if="myForm.$submitted && myForm.pwd.$error.required">Please fill field above<br></span> 
              Confirm Password:<input type="password" class="form-control input-sm glyphicon glyphicon-ok" name="pwd2" ng-minlength="3" ng-model="pwd2" required placeholder="Confirm Password"/> 
              <span style="color:red" ng-show="myForm.pwd2.$error.minlength">password cannot be less than 3 letters</span> 
              <span style="color:red" class="error" ng-if="myForm.$submitted && myForm.pwd2.$error.required">Please fill field above<br></span><br/> 

              Gender: <input type="radio" name="female" ng-model="color1" value="female" ng-required="!color1"/>Female <input type="radio" name="male" ng-model="color1" value="male" ng-required="!color1"/>Male <br/><br/> 

              Mobile:<input type="text" class="form-control input-sm" name="mobile" ng-pattern="/^[7-9][0-9]{9,9}$/" ng-model="mobile" required placeholder="Mobile"/> 
              <span style="color:red" ng-show="myForm.mobile.$error.pattern">Please enter a valid mobile number</span><br/> 
              <span style="color:red" class="error" ng-if="myForm.$submitted && myForm.mobile.$error.required">Please fill field above<br></span> 

              Email:<input type="email" class="form-control input-sm" name="email" ng-pattern="/\[email protected]\S+\.\S+/" ng-model="email" required placeholder="Email"/> 
              <span style="color:red" ng-show="myForm.email.$error.pattern">Invalid email address</span><br/> 
              <span style="color:red" class="error" ng-if="myForm.$submitted && myForm.email.$error.required">Please fill field above<br></span> 

              Address:<textarea class="form-control input-sm" name="address" ng-model="address" required placeholder="Address"></textarea> 
              <span style="color:red" ng-show="myForm.address.$error.require == true">Address cannot be empty</span> 
              <span style="color:red" class="error" ng-if="myForm.$submitted && myForm.address.$error.required">Please fill field above<br></span><br/> 

              Street:<input type="text" class="form-control input-sm" name="street" ng-model="street" required placeholder="Street"/> 
              <span style="color:red" class="error" ng-if="myForm.$submitted && myForm.street.$error.required">Please fill field above<br></span><br/> 

              Area:<input type="text" class="form-control input-sm" name="area" ng-model="area" required placeholder="Area"/> 
              <span style="color:red" class="error" ng-if="myForm.$submitted && myForm.area.$error.required">Please fill field above<br></span><br/> 

              City: <select ng-model="citySelection" class="form-control" ng-options="city for city in cityArray" required> 
               <option value="">(Pick One)</option> 
              </select> 
              <span style="color:red" class="error" ng-if="myForm.$submitted && myForm.citySelection.$error.required">Please fill field above<br></span><br/> 

              State: <input type="text" class="form-control input-sm" name="state" ng-model="state" required placeholder="State"/> 
              <span style="color:red" class="error" ng-if="myForm.$submitted && myForm.state.$error.required">Please fill field above<br></span><br/> 

              Country: <input type="text" class="form-control input-sm" name="country" ng-model="country" required placeholder="Country"/> 
              <span style="color:red" class="error" ng-if="myForm.$submitted && myForm.country.$error.required">Please fill field above<br></span><br/> 

              Pin:<input type="text" class="form-control input-sm" ng-pattern="/^\d{6,6}$/" name="pin" ng-model="pin" required placeholder="Pin"/> 
              <span style="color:red" ng-show="myForm.pin.$error.pattern">Only six-digit number is allowed</span> 
              <span style="color:red" class="error" ng-if="myForm.$submitted && myForm.pin.$error.required">Please fill field above<br></span><br/> 

              <button class="form-control btn btn-success" type="submit" ng-click="submit($event)">Submit</button> 
            </form> 
           </div> 
          </div> 
         </div> 
        </div> 
       </div> 
      </div> 
     </div>    
    </body> 
</html> 
+0

angularjs!= jQuery的 – Vamsi

+0

@Vamsi V你是什么意思 – kittu

+0

采用了棱角分明的时候忘记了jQuery,使用'NG-模型=“了selectedValue”'在选择标签,它会自动控制器,你当更新的价值选择其他选项,您可以使用'$ scope.selectedValue'获取控制器中选择框的值 – Vamsi

回答

2

这里更新plunker

你忘了分配name属性选择和使用它的错误显示: -

例如name="city",并用它为ng-if="myForm.$submitted && myForm.city.$error.required"

City: <select name="city" ng-model="citySelection" class="form-control" ng-options="city for city in cityArray" required> 
    <option value="">(Pick One)</option> 
</select> 
<span style="color:red" class="error" ng-if="myForm.$submitted && myForm.city.$error.required">Please fill field above<br></span><br/> 

在单选按钮的情况下,你没有使用相同的name属性name="gender"为相同,并没有提及错误<span> :-)

Gender: <input type="radio" name="gender" ng-model="color1" value="female" ng-required="!color1"/>Female <input type="radio" name="gender" ng-model="color1" value="male" ng-required="!color1"/>Male <br/><br/> 
<span style="color:red" class="error" ng-if="myForm.$submitted && myForm.gender.$error.required">Please fill field above<br></span> 
1

对于城市场,分配一个名称的选择标记,并在验证运行该名称标签服务错误...即(myForm.sample。$错误.required)

Here is the code for drop down validation 

    <select name="sample" ng-model="citySelection" class="form-control" ng-options="city for city in cityArray" required> 
    <option value="">(Pick One)</option> 
</select> 
<span style="color:red" class="error" ng-if="myForm.$submitted && myForm.sample.$error.required">Please fill field above<br></span><br/> 
+0

对单选按钮也是如此 –

2

在单选按钮和城市下拉验证的情况下,您应该删除控制器代码中的$event.preventDefault();。这完全解决了这个问题。

见工作plunkr“http://plnkr.co/edit/TnIfWhcKESRHJFPPisNl?p=preview

添加以下性别在屏幕上显示的信息,改变它的name属性为一个单一的共同价值说“性别”:

Gender: <input type="radio" name="gender" ng-model="color1" value="female" ng-required="!color1"/>Female <input type="radio" name="gender" ng-model="color1" value="male" ng-required="!color1"/>Male <br/><br/> 

<span style="color:red" ng-show="myForm.gender.$error.require == true">Select Gender</span> 
<span style="color:red" class="error" ng-if="myForm.$submitted && myForm.gender.$error.required">Please fill field above<br></span><br/> 

而且添加城市以下的在屏幕上显示的消息,并添加“name”属性城市name = "city"

City: <select ng-model="citySelection" name = "city" class="form-control" ng-options="city for city in cityArray" required> 
               <option value="">(Pick One)</option> 
              </select> 
<span style="color:red" ng-show="myForm.city.$error.require == true">city cannot be empty</span> 

<span style="color:red" class="error" ng-if="myForm.$submitted && myForm.city.$error.required">Please fill field above<br></span><br/> 
+0

对城市不起作用 – kittu

+0

再次更新了plunkr检查。 –

+0

也有任何想法如何匹配密码? – kittu

0

使用本

City: <select name="dd" ng-model="citySelection" class="form-control" ng-options="city for city in cityArray" required> 
               <option value="">(Pick One)</option> 
              </select> 
              <span style="color:red" class="error" ng-if="myForm.$submitted && myForm.dd.$error.required">Please fill field above<br></span><br/> 
+0

你能解释一下,它与过去3个答案有什么不同吗? –

+0

将名称分配给下拉菜单并在myForm.name中使用相同的名称$ error.required –