2015-05-15 43 views
0

我想驗證我的形式,如果用戶鍵入空格AUR該字段留空驗證..的JavaScript的空間和空場

的問題是,這個碼字完美的,當我把空間的文本框,顯示驗證..但如果空間是空的,重定向到下一個頁面

這是我的控制器

$scope.valid= function(){ 
     var fname = $("#fname").val(); 
     var lname = $("#lname").val(); 
     var email = $("#email").val(); 
     var contact = $("#contact").val(); 


     if(fname.indexOf(" ") !== -1) 
     { 
      $scope.modalvalue = "Spaces are not allowed";registermodal.show(); 
     } 
     else if(lname.indexOf(" ") !== -1){ 
      $scope.modalvalue = "Spaces are not allowed";registermodal.show(); 
     } 
     else if(lname.indexOf(" ") !== -1){ 
      $scope.modalvalue = "Spaces are not allowed";registermodal.show(); 
     } 
     else if(contact.indexOf(" ") !== -1){ 
      $scope.modalvalue = "Spaces are not allowed";registermodal.show(); 
     } 
    } 

這是我resigter.html

<ons-row style="margin-top:45px;text-align: center"> 
      <ons-col> 
       <div> 
        <input class="textbox" id="fname" name="fname"  ng-model="fname" type="text" placeholder="First name" ng-trim='false' required/> 
       </div> 
      </ons-col> 
     </ons-row> 
     <ons-row style="margin-top:15px;text-align: center"> 
      <ons-col> 
       <div> 
        <input class="textbox" id="lname" ng-model="lname" name="lname" type="text" placeholder="Last name" required/> 
       </div> 
      </ons-col> 
     </ons-row> 
     <ons-row style="margin-top:15px;text-align: center"> 
      <ons-col> 
       <div> 
        <input class="textbox" id="email" ng-model="email" type="email" placeholder="Email" required/> 
       </div> 
      </ons-col> 
     </ons-row> 
     <ons-row style="margin-top: 15px;font-family:mai;font-size: 1.2em;text-align: center"> 
      <ons-col> 
       <div class=""> 
        <span style="padding: 10px"> 
         <input id="radio1" type="radio" ng-model="gender" value="1" checked="checked"><label for="radio1">Male</label> 
        </span> 
        <span style="padding: 10px"> 
         <input id="radio1" type="radio" ng-model="gender" value="2" ><label for="radio1">Female</label> 
        </span> 
       </div> 
      </ons-col> 
     </ons-row> 
     <ons-row style="margin-top:15px;text-align: center"> 
      <ons-col> 
       <div> 
        <input class="textbox" ng-model="password" type="password" placeholder="Password" required/> 
       </div> 
      </ons-col> 
     </ons-row> 

     <ons-row style="margin-top:15px;text-align: center"> 
      <ons-col> 
       <div> 
        <input class="textbox" id="contact" ng-model="contact" type="text" placeholder="Contact Number" required/> 
       </div> 
      </ons-col> 
     </ons-row> 

     <ons-row style="text-align:center;margin-top:10px"> 
      <ons-col> 
       <input type="button" class="loginbutton buttoncolor" value="Register" ng-click="register() ; valid()" /> 
      </ons-col> 
     </ons-row> 

    <ons-row> 
     <ons-modal var="registermodal"> 
      <div>{{modalvalue}}</div> 
      <ons-button ng-click="showlogin1()" style="margin:20px;">Close</ons-button> 
     </ons-modal> 
    </ons-row> 
+0

檢查空字符串'」 「'。但是......在名字(「Mary Ann」)或姓氏(「Van Buren」)中不允許有空格?更好地使用正則表達式來測試任何空間序列(包括零), – usr2564301

回答

0

你爲什麼不只是試試這個簡單的approche:

if(fname!= "" && fname!= " ") 
    { 
     $scope.modalvalue = "Spaces are not allowed";registermodal.show(); 
    } 

,爲別人做同樣的文本框

0

使用trim()在JavaScript

if(!fname || fname.trim() == ""){ 
    $scope.modalvalue = "Spaces are not allowed"; 
    registermodal.show(); 
} 
... 
+0

我更新了我的答案請檢查 –