写一个NG-模式不允许空格和特殊字符 [英] write an ng-pattern to disallow whitespaces and special characters

查看:992
本文介绍了写一个NG-模式不允许空格和特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到一个角度NG-模式来检查用户名没有空格或特殊字符。如果输入空格或特殊字符以下表格返回false。但是,一旦你进入A-Z,a-z或0-9为真。我曾尝试NG-模式=/ [^ \\ s] + /和\\ S和[^]但他们没有什么区别。

I am trying to get an angular ng-pattern to check that a username has no whitespaces or special characters. The following form return false if you enter whitespaces or special characters. However, it becomes true as soon as you enter a-z, A-z or 0-9. I have tried ng-pattern="/[^\s]+/" and \S and [^ ] but they make no difference.

<form name="myform">
  valid? {{ myform.$valid }}
  <input type="text" name="username" ng-model="username" ng-pattern="/[a-zA-Z0-9^ ]/" required/>
</form>

下面是在普拉克格式为:<一href=\"http://plnkr.co/edit/6T78kyUgXYfNAwB4RHKQ?p=$p$pview\">http://plnkr.co/edit/6T78kyUgXYfNAwB4RHKQ?p=$p$pview

Here's the form in a plunk: http://plnkr.co/edit/6T78kyUgXYfNAwB4RHKQ?p=preview

推荐答案

请尝试以下方式:

/^[a-zA-Z0-9]*$/

这仅允许字母数字字符。

This allows only alphanumeric characters.

这篇关于写一个NG-模式不允许空格和特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆