gpt4 book ai didi

javascript - ng-pattern 不适用于正则表达式

转载 作者:行者123 更新时间:2023-11-28 00:22:54 26 4
gpt4 key购买 nike

我的表单具有以下内联验证

<form name="coForm" novalidate> 
<div>
<label>Transaction: </label>
<input type="text" name="transaction" class="form-control"
placeholder="<Direction> <Message Type>, ex.:OUT X12214"
ng-model="newco.transaction"
ng-pattern=/(^(IN )([A-Za-z0-9 &_]+))|(^(OUT )([A-Za-z0-9 &_]+))/
required>

<span style="color:red"
ng-show="coForm.transaction.$dirty ||
coForm.transaction.$invalid">
<span ng-show="coForm.transaction.$error.pattern">
Enter correct value for Transaction
</span>
<span ng-show="coForm.transaction.$error.required">
Transaction is required.
</span>
</div>
</form>

但验证仅适用于required,不适用于pattern

Here is the fiddle

以下是积极匹配的几个示例:

OUT X12214
In X12850
IN ARRANT
OUT CORREC&TRANSLEG
OUT TEST_TEST
IN TEST2&TEST2

最佳答案

您缺少 ng-pattern 属性上的双引号,而且您还没有在页面上初始化 Angular,您需要添加 ng-app 才能在页面上运行 Angular

标记

<form name="coForm" novalidate ng-app="">
<div>
<label>Transaction:</label>
<input type="text" name="transaction" class="form-control" placeholder="<Direction> <Message Type>, ex.:OUT X12214" ng-model="newco.transaction"
ng-pattern="/(^(IN )([A-Za-z0-9 &_]+))|(^(OUT )([A-Za-z0-9 &_]+))/" required>
<span style="color:red" ng-show="coForm.transaction.$dirty || coForm.transaction.$invalid">
<span ng-show="coForm.transaction.$error.pattern">Enter correct value for Transaction</span>
<span ng-show="coForm.transaction.$error.required">Transaction is required.</span>
</span>
</div>
</form>

JsFiddle

关于javascript - ng-pattern 不适用于正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29814234/

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