2015-06-22 96 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 –