How to regex match entire string instead of a single character

前端 未结 2 760
暖寄归人
暖寄归人 2021-01-21 09:25

I am trying to implement \"alpha\" validation on Arabic alphabet characters input, using the JavaScript regex /[\\u0600-\\u06FF]/ as instructed in this post. I want

2条回答
  •  迷失自我
    2021-01-21 09:53

    You can explicitly allow some keys e-g: numpad, backspace and space, please check the code snippet below:

    function restrictInputOtherThanArabic($field)
    {
      // Arabic characters fall in the Unicode range 0600 - 06FF
      var arabicCharUnicodeRange = /[\u0600-\u06FF]/;
    
      $field.bind("keypress", function(event)
      {
        var key = event.which;
    
        // 0 = numpad
        // 8 = backspace
        // 32 = space
        if (key==8 || key==0 || key === 32)
        {
          return true;
        }
    
        var str = String.fromCharCode(key);
        if ( arabicCharUnicodeRange.test(str) )
        {
          return true;
        }
    
        return false;
      });
    }
    
    // call this function on a field
    restrictInputOtherThanArabic($('#firstnameAr'));
    

提交回复
热议问题