JavaScript正则表达式可验证输入是否超过0个字符 [英] JavaScript regex to validate an input is more than 0 characters
问题描述
如果这很简单,请原谅我,但是我在Google和Google上搜索,找不到很好的例子.我对正则表达式不太满意,只需要验证输入的字符数是否超过1个即可(即不为空).我正在使用Angular ng模式.
Forgive me if this is super simple, but I googled and googled and couldn't find a good example. I'm not that great with regex and just need to valid that an input has more than 1 character (i.e. is not blank). I'm using Angular ng-pattern.
<input type="text" ng-model="username" ng-pattern="/regex/">
我需要验证输入中是否有内容(不为空).我使用了两个示例,但是问题是一旦您清除输入,angular仍然会看到该模式有效.一旦输入再次为空,我需要它失败,但不是在第一次加载时.一如既往地感谢所有帮助.
I need to verify that there as something in the input (not empty). I've used a couple of example, but the issue is once you clear the input, angular is still seeing the pattern as valid. I need it to fail once the input is empty again, but not on first load. As always all help is much appreciated.
我基本上需要一个正则表达式,它将验证该字段不是空白.这是 plnkr
I basically need a regex that will verify the field is not blank. Here is a plnkr
推荐答案
您可以为此使用必需
:
<input type="text" ng-model="username" name="username" required>
请参阅以下pluckr分支: http://plnkr.co/edit/VwY1WPdCocKHwww69ZSj?p=preview
See this pluckr fork: http://plnkr.co/edit/VwY1WPdCocKHwww69ZSj?p=preview
更多文档资料: http://docs.angularjs.org/api/ng.directive:input#usage_parameters
如果您真的要使用 ng-pattern
,则应添加输入的开始/结束( ^
和 $
):
If you REALLY want to use ng-pattern
you should add start/end of input (^
and $
):
<input type="text" ng-model="username" ng-pattern="/^$/">
但是请记住,这将验证空白输入(输入值与模式匹配).
But remember, this will validate a blank input (the input value matches the pattern).
这篇关于JavaScript正则表达式可验证输入是否超过0个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!