Javascript RegExp dosn't recognize apostrophe on mobile iOS - Stack Overflow

I have a form with three input.The first is only number and for the other two I can accept all letters

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");

    });

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.

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}"

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");

    });

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.

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}"

Share Improve this question asked Aug 29, 2019 at 14:12 GiuGiu 2951 gold badge7 silver badges23 bronze badges 3
  • 2 I think it is because of how apostrophes are managed by the iOS when typing. It turns them into curly apos. Replace new RegExp("^[a-zA-ZÀ-ÿ ']{2,60}$") with new RegExp("^[a-zA-ZÀ-ÿ '‘’]{2,60}$") – Wiktor Stribiżew Commented Aug 29, 2019 at 14:15
  • I have changed the value of the RegExp but it doesn't work – Giu Commented Aug 29, 2019 at 14:17
  • I have changed the order of chars and it works! So, at the end, the correct string is new RegExp("^[a-zA-ZÀ-ÿ ‘’']{2,60}$") – Giu Commented Aug 29, 2019 at 15:05
Add a ment  | 

2 Answers 2

Reset to default 6

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.

In the Regex and Smart Punctuation in iOS post, the author suggests adding ‘’ to the regex:

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}$/;

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.

Thanks to @Wiktor Stribiżew for the help.
The correct string is new RegExp("^[a-zA-ZÀ-ÿ ‘’']{2,60}$")
I also have find this link that can help.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745561804a4633150.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信