簡體   English   中英

如何在Java腳本或angular js中執行.rw文件驗證?

[英]How to perform .rw file validation in java script or angular js?

這是此代碼,當單擊按鈕時成功進行了空白空間驗證,但是.rw文件驗證未發生,顯示為[TypeError:$ scope.defaultMsrPath.contains is not function]錯誤。<---------- ------------ HTML文件---------------->

<form name="myformA" novalidate>    

 <div class="modal-body">
 <h4>Enter the path to save the CPU MSR Information</h4>
 <input type="text" name="myFieldA" ng-model="defaultMsrPath" required=required class="input-large searchPath">
 <p ng-show="showMsgA && myformA.myFieldA.$error.required" style="color:#b22727">Above field should not be empty</p>
 </div>

 <div class="modal-footer">
  <button type="button" class="btn" ng-click="cpuMsrSavePath()">Save</button>
  <button type="button" class="btn" data-dismiss="modal">Cancel</button>
 </div>

 </form>

<----------------。js文件----------------->

$scope.showMsgA = false;

$scope.cpuMsrSavePath = function(){

     if (!$scope.myformA.$valid) {
         $scope.showMsgA = true;
      }else if ($scope.defaultMsrPath.contains('.rw')&& $scope.myformA.$valid){
          alert();
      }

}

更新的答案

在html文件中,將defaultMsrPath變量傳遞給您cpuMsrSavePath函數

<button type="button" class="btn" ng-click="cpuMsrSavePath(defaultMsrPath)">Save</button>

並在js文件中

$scope.cpuMsrSavePath = function(defaultMsrPath){
 if (!$scope.myformA.$valid) {
     $scope.showMsgA = true;

  }else if (defaultMsrPath.contains('.rw')&& $scope.myformA.$valid){
      alert();
  }

}

下面是當您使用文件輸入字段時

您可以像這樣更改您的html

<input type='file' class="form-control" ngChange='onChange($event)' multiple='false' name='file' >

然后在onChange函數句柄驗證(我已經使用lodash _.endsWith(_.toLower ))中,您可以嘗試自己的。

onChange(event) {
    this.fileElement = event.srcElement;
    let files = this.fileElement.files;
        if(!_.endsWith(_.toLower(files[0].name), '.rw')) {
            // not .rw file
        }
}

為了執行文件驗證,我發現了另一種方法

var fname = "the file name here.xml"; 
var re = /(\.xml|\.rw|\.csv|\.html)$/i;
if(!re.exec(fname))
{
alert("File extension not supported!");
}
else
{
alert("supported")
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM