用模式限制输入

时间:2015-01-10 04:41:38

标签: javascript

我想在文本字段中将用户输入作为他们的名字。我想用两个表达式来做到这一点:

  pattern="[/\D/gi]"
  // and
  pattern="[/\s/gi]"

但它需要输入而没有空间。我希望输入包含空格。

2 个答案:

答案 0 :(得分:0)

尝试使用此正则表达式:[a-zA-Z ]+

<input name="userName" pattern="[a-zA-Z ]+" />

查看this site,您会发现它对将来的验证非常有用。

答案 1 :(得分:0)

试试这个: 在任何名称之前不允许任何空格,并且在名称

之后也允许

<input name="userName" pattern="^[a-zA-Z][a-zA-Z\s]+$" required />

<强> explantion

     ^  //start of the string
    [a-zA-Z]  //first char must be a letter (lowercase & uppercase)
    [a-zA-Z\s]+  //and 2nd char - more then one  must be letter (lowercase & uppercase) and space
    $  //end of the string.
相关问题