为什么我的正则表达式没有被触发?输入任何内容时,将显示第一条错误消息“请输入经度”。如果我为“longtitude”输入了明显错误的值,则不会显示错误消息。
<div class="row">
<div class="col span-1-of-3">
<label for="name">Longtitude</label>
</div>
<div class="col span-2-of-3">
<input type="text" ng-pattern="/^[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/" ng-model="report.lon" name="lon" id="longtitude" placeholder="Enter the longtitude" required />
</div>
<div ng-messages="submitReport.lon.$error" ng-if="submitReport.lon.$dirty">
<div ng-message="required">Please enter the longtitude</div>
<div ng-message="lon">Your longtitude is invalid</div>
</div>
<div ng-show="submitReport.lon.$error.pattern" ng-if="submitReport.lon.$dirty">
<div ng-message="required">Please enter the longtitude</div>
<div ng-message="lon">Your longtitude is invalid</div>
</div>
</div>
答案 0 :(得分:2)
它对我有用:https://plnkr.co/edit/WPjGeylGESDexh7Q6UKJ?p=preview
另请注意您的经度拼写不正确。
修改强>
代码中的问题是
<div ng-message="lon">Your longitude is invalid</div>
应该是
<div ng-message="pattern">Your longitude is invalid</div>