gpt4 book ai didi

javascript - 模糊触发时如何限制对字段的验证?

转载 作者:行者123 更新时间:2023-11-30 17:23:19 26 4
gpt4 key购买 nike

以下代码正确地对文本字段(时间格式 00:00:00)应用验证。但是我需要在退出字段后验证输入标签,此时它会在用户输入时进行验证。知道如何解决这个问题吗?

  <label for="operativeToTime">Operative to time</label>
<input name="operativeToTime"
ng-model="deviceDetails.operativeToTime"
type="text"
ng-pattern="/^(?:2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9]$/"
ng-required="true">
<div class="error-container" ng-show="editDeviceForm.operativeToTime.$invalid">
<span class="error" ng-show="editDeviceForm.operativeToTime.$error.required">Operative to time is required</span>
<span class="error" ng-show="editDeviceForm.operativeToTime.$invalid">+++++++++++++Wrong format</span>
</div>

最佳答案

你想要的是内置的 ng-blur:https://docs.angularjs.org/api/ng/directive/ngBlur

<label for="operativeToTime">Operative to time
<input name="operativeToTime"
ng-model="deviceDetails.operativeToTime"
type="text"
ng-required="true"
ng-blur="validateInput(this)"/>
<div class="error-container" ng-show="editDeviceForm.operativeToTime.$invalid">
<span class="error" ng-repeat="error in errors" ng-bind="error"></span>
</div>

更新 - 根据 OP 的要求添加了 JS:您需要在这些行上添加一些内容以将错误绑定(bind)到您的范围:

$scope.errors = [];
$scope.validateInput = function(element){
var validate1 = //your logic for validation here
if(!validate1){$scope.errors.push("Error message 1");}
var validate2 = //your logic for validation here
if(!validate2){$scope.errors.push("Error message 2");}
};

关于javascript - 模糊触发时如何限制对字段的验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24738031/

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