【问题标题】:How do I prevent blank characters in my textarea in javascript?如何在 javascript 中防止文本区域中出现空白字符?
【发布时间】:2021-01-15 14:32:53
【问题描述】:

我有一个不允许用户加倍空格或输入的文本区域。我看到的问题是一些用户使用https://www.compart.com/en/unicode/U+2800 创建空白空间。如何在 Javascript (React) 中防止这种情况

【问题讨论】:

    标签: javascript reactjs frontend textarea


    【解决方案1】:

    使用正则表达式,您可以验证我认为的值。

    类似这样的:

    /[\s\u2800]{2,}/gmu
    

    其他资源

    link to codesandbox example

    Link to regex101 with my test

    定义

    \s => any whitespace character
    \u2800 => Matches the unicode character with the given hex value
    {2,} => 2 or more
    /g => don't return after first match
    /m => multi line
    /u => match with full unicode (maybe useless)
    

    【讨论】:

      猜你喜欢
      • 2016-02-04
      • 1970-01-01
      • 2013-06-20
      • 1970-01-01
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 2011-12-18
      • 1970-01-01
      相关资源
      最近更新 更多