简体   繁体   English

带有ng-pattern的AngularJS验证可在桌面浏览器中使用,但不能在移动设备上使用

[英]AngularJS validation with ng-pattern works in desktop browser but not mobile

I've written a very simple regular expression to ensure that some user input is alpha numeric with a single space as follows: 我编写了一个非常简单的正则表达式,以确保某些用户输入是带有单个空格的字母数字,如下所示:

        <input type="text" ng-minlength="5" ng-maxlength="8" ng-pattern="/^[0-9A-Z]*\s[0-9A-Z]*$/gi" required   placeholder="Postcode" ng-model="addr.postcode"  class="form-control" name="postcode" />
        <div class="col-xs-12" ng-messages="myform.postcode.$error" ng-if="myform.$submitted || myform.postcode.$dirty">
            <div class="text-warning" ng-message="minlength">Postcode is too short.</div>
            <div class="text-warning" ng-message="pattern">Must contain a space.</div>
            <div class="text-warning" ng-message="maxlength">Postcode is too long.</div>
        </div>

When I test this in chrome on the desktop it works a charm. 当我在台式机上以chrome进行测试时,它很有魅力。 I get a validation error if my input does not contain a space. 如果我的输入不包含空格,则会收到验证错误。 The error clears when I enter a space. 输入空格后,错误将清除。 eg "AB12 3CD" passes and "AB123CD" fails which is as I would expect. 例如“ AB12 3CD”通过而“ AB123CD”失败,这正是我所期望的。

If I open the same page on a mobile device (I've tested with iPhone 5 & 6 and a Windows Phone 8.1) the pattern always fails regardless of input. 如果我在移动设备上打开了同一页面(我已经在iPhone 5和6和Windows Phone 8.1上进行了测试),则无论输入如何,该模式始终会失败。 eg both "AB12 3CD" and "AB123CD" fail. 例如“ AB12 3CD”和“ AB123CD”均失败。

It was the global g option. 这是全局g选项。 If I change my regular expression to 如果我将正则表达式更改为

ng-pattern="/^[0-9A-Z]*\s[0-9A-Z]*$/i"

it works on both mobile and desktop versions 它适用于移动和台式机版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM