gpt4 book ai didi

javascript - splitPinCodes.split (',' )给出错误

转载 作者:行者123 更新时间:2023-11-28 06:28:49 27 4
gpt4 key购买 nike

我在验证输入时遇到错误。我在输入文本区域中得到以逗号分隔的 6 位邮政编码。我在输入文本区域应用了 ng-change="convertToArray()"( Angular 方法)。

当我输入超过 6 位数字且不带逗号(,) 时,会出现错误

HTML 文件

$scope.convertToArray=function(){
var splitPinCodes = $scope.cluster.pincode;
$scope.toArray = splitPinCodes.split(',');

}
<div class="page page-tasks ng-scope main-div ">
<div class=" callout-elem-info ng-scope cntrl-div"
data-ng-controller="mobilizationsettings">
<form name="ClusterInfoForm" class="add-task table form-validate" novalidate>
<div class="ng-scope left-div">

<div layout layout-sm="column">
<h3>Cluster No.1</h3>

<md-input-container flex class="clusterName">
<label>Cluster Name</label> <input id="inputCLusterName"
ng-pattern="/^[a-zA-Z ]*$/" type="text" ng-model="cluster.Name"
maxlength="40" placeholder="Enter Cluster Name"
class="form-control required" required /> </md-input-container>
</div>

<div layout layout-sm="column">
<md-input-container flex> <label>Cluster
PinCodes</label> <input id="inputCLusterPincodes"
ng-pattern="/^[1-9][0-9]{5}(?:,[1-9][0-9]{5})*$/" type="text"
ng-model="cluster.pincode" ng-change="convertToArray()"
placeholder="Enter Cluster PinCode" class="form-control " /> </md-input-container>

<div class="addPinCode-div">
<md-button required ng-model="newEntry"
class="btn-width-medium md-raised md-primary btn-lg md-button md-default-theme md-button addPinCodeButton"
ng-click="addNewPinCode($data,$event)"> ADD
PINCODE(S) </md-button>
</div>
&nbsp;
</div>

所以请帮助我寻找其他替代方案。

最佳答案

如果未找到分隔符,则 split 将不执行任何操作。重写如下,它应该可以工作:

$scope.convertToArray=function(){
if($scope.cluster.pincode.indexOf(',')==-1){
$scope.toArray = [$scope.cluster.pincode];
}else{
var splitPinCodes = $scope.cluster.pincode;
$scope.toArray = splitPinCodes.split(',');
}
}

编辑:

when I entered more than 6 digit without comma(,) it gives error

是的,这就是你的 ng-pattern 所说的:“如果超过 6 个字符且没有逗号,则崩溃”。

关于javascript - splitPinCodes.split (',' )给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34875078/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com