编写一个 ng-pattern 来禁止空格和特殊字符 [英] write an ng-pattern to disallow whitespaces and special characters

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

问题描述

我正在尝试获取一个有角度的 ng-pattern 来检查用户名是否没有空格或特殊字符.如果您输入空格或特殊字符,以下形式将返回 false.但是,一旦您输入 a-z、A-z 或 0-9,它就会变为真.我试过 ng-pattern="/[^\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>

这是一个简单的表格:http://plnkr.co/edit/6T78kyUgXYfNAwB4RHKQ?p=preview

推荐答案

尝试以下模式:

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

这仅允许使用字母数字字符.

This allows only alphanumeric characters.

这篇关于编写一个 ng-pattern 来禁止空格和特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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