在AngularJS中输入文本自定义验证

时间:2015-02-06 09:38:06

标签: angularjs

有以下代码:

<input class="form-control" id="secret_key" type="text" name="secret_key" ng-model="hotel.secret_key" ng-required="true" ng-minlength="8" placeholder="Secret Key" />

我可以验证这个字段的长度,它有效。但现在我必须验证这个长度是否包含至少一个数字,一个小写字母和一个大写字母。我怎么能用AngularJS做到这一点?提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以通过RegExp在输入模型上设置简单的ng-pattern验证模式,如下所示:

<input ng-pattern="/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$/" name="text" type="text" ng-model="formData">

这将验证输入是否包含至少 1个小写字符 1个大写字符 1个整数

Here is an example让您试用上述代码。

如果您有兴趣了解RegExp模式,here is the breakdown