Javascript RegExp在移动iOS上无法识别撇号 [英] Javascript RegExp dosn't recognize apostrophe on mobile iOS

查看:86
本文介绍了Javascript RegExp在移动iOS上无法识别撇号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有三个输入的表格.
第一个是唯一的数字,对于其他两个,我可以接受从A到Z,空格和撇号的所有字母.

I have a form with three input.
The first is only number and for the other two I can accept all letters from A to Z, space and apostrophe.

HTML :

<input type="text" name="number" id="number"/>
<input type="text" name="name" id="name"/>
<input type="text" name="surname" id="surname"/>

JavaScript :

$("body").on("click","#button",function(){

        let regexMessaggio = new RegExp("^[0-9]{1,5}$");
        if (!regexMessaggio.test($('#number').val())){
            alert("error");     
            return false;
        }

        let regexMessaggio2 = new RegExp("^[a-zA-ZÀ-ÿ ']{2,60}$");
        if (!regexMessaggio2.test($('#name').val())){
            alert("error");     
            return false;
        }
        if (!regexMessaggio2.test($('#surname').val())){
            alert("error"); 
            return false;
        }

        alert("OK");

    });

在台式机上的每个浏览器中,此代码均有效,但在iOS上无效.我已经尝试过chrome和safari,但无法识别撇号.

In every browser on desktop this code works, but not on iOS. I have tried on chrome and safari but it doesn't recognize the apostrophe.

我还尝试使用以下方法更改正则表达式:
-"^([a-zA-ZÀ-ÿ'] {2,60})+ $"
-/^ [a-zA-ZÀ-ÿ'] {2,60} $/
-"[a-zA-ZÀ-ÿ'] {2,60}"

I have also tried to change the regular expression with:
- "^([a-zA-ZÀ-ÿ ']{2,60})+$"
- /^[a-zA-ZÀ-ÿ ']{2,60}$/
- "[a-zA-ZÀ-ÿ ']{2,60}"

推荐答案

如果处于打开状态,所有直的单撇号都会自动转换为卷曲的撇号.即使您可以访问UI文本输入字段的smartQuotesType属性,用户也可以在其中粘贴大括号,您应该对此加以考虑.

If the "Smart Punctuations" are switched on all straight single apostrophes are automatically converted to curly apostrophes. Even if you have access to the smartQuotesType property of UI text input fields, users may paste curly apostrophes there and you should account for them.

正则表达式和智能标点iOS 帖子中,作者建议在正则表达式中添加‘’:

let regexMessaggio2 = /^[a-zA-ZÀ-ÿ ‘’']{2,60}$/;

或者,如果存在任何编码问题,请使用十六进制表示形式:

Or, in case there are any encoding issues use hex representations:

let regexMessaggio2 = /^[a-zA-ZÀ-ÿ \u2018\u2019']{2,60}$/;

请注意,当模式为静态(即,您没有将任何变量传递给regex模式)时,regex文字符号(/.../)比RegExp构造函数符号更可取.

Note that a regex literal notation (/.../) is preferable to the RegExp constructor notation when the pattern is static, i.e. you are not passing any variables to the regex pattern.

这篇关于Javascript RegExp在移动iOS上无法识别撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆