How to regex match entire string instead of a single character

前端 未结 2 761
暖寄归人
暖寄归人 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:45

    You need to add ^ and $ anchors to the regular expression, as well as a + to allow multiple characters.

    Try this:

    /^[\u0600-\u06FF]+$/
    

    I'm not sure if "Arabic spaces" that you mentioned are included in the character range there, but if you want to allow white space in the string then just add a \s inside the [] brackets.

    0 讨论(0)
  • 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'));
    
    0 讨论(0)
提交回复
热议问题