简体   繁体   English

angularjs ng-pattern根据正则表达式条件更改元素的css样式

[英]angularjs ng-pattern change css style of elements based on regex condition

In my angularjs form, I am checking the password pattern with a regex that checks for multiple conditions. 在我的angularjs表单中,我正在使用用于检查多个条件的正则表达式检查密码模式。 Based on different condition checks of this regex, I want to change the css for one of the elements. 基于此正则表达式的不同条件检查,我想更改元素之一的css。

Jsfiddle link Here . jsfiddle链接在这里。

The source model that inspired this is from here . 启发这一点的源模型是从这里开始的

This is my function to check the pattern against the regex : 这是我根据正则表达式检查模式的功能:

$scope.passPatternCheck= (function() {
    var regexp = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!#\$%&'\(\)\*\+,\-\.\/:;\<=\>\?@\[\\\]\^_`\{\|\}~]?)[A-Za-z\d!#\$%&'\(\)\*\+,\-\.\/:;\<=\>\?@\[\\\]\^_`\{\|\}~]{8,}/;
    return {
        test: function(value) {               
            return regexp.test(value);
        }
    };
})();

Some notes: 一些注意事项:

  • this regex don't check for the starting/ending spaces; 这个正则表达式不检查开始/结束空间; this should also be adjusted 这也应该调整
  • the ng-if that shows/hides the div of the conditions list is not currently working. 显示/隐藏条件列表的div的ng-if当前不起作用。

question: 题:

I want to reproduce the same behaviour from the godaddy site account creation page. 我想从godaddy网站帐户创建页面重现相同的行为。 So the goal is to have the same css behaviour according to the condition completed. 因此,目标是根据完成的条件具有相同的CSS行为。 在此处输入图片说明

update 2: 更新2:

my submit button of the form theForm should get disabled if any of the fields that are required are not filled out or an error in the patterns occurred: 我提交表单的按钮theForm应该得到禁用如果有任何要求的字段不填写或模式发生了错误:

<button type="submit" class="btn btn-primary"ng-disabled="theForm.$invalid" ng-click="submit()">
</button>

but with the Shantanu's answer , this is not yet possible. 但是,根据Shantanu的回答 ,这还不可能。

Adding one regex for ng-pattern will not achieve it. ng-pattern添加一个正则表达式将无法实现。 Because if regex test fails then we would not know exactly which case of all those checkbox conditions failed. 因为如果正则表达式测试失败,那么我们将无法确切知道所有这些复选框条件中的哪种情况都失败了。 You've to handle different regex for each conditions & check them in either custom directives (using $validators ) and create custom validators for that each validation OR you can check those condition on change of password input field inside controller & create one object with different properties as Boolean values for those validation conditions. 您必须为每个条件处理不同的正则表达式,并在自定义指令中检查它们(使用$validators )并为每个验证创建自定义验证器, 或者您可以在控制器内部更改密码输入字段时检查这些条件并创建一个具有不同对象的对象属性作为这些验证条件的布尔值。

I've created jsfiddle for 2nd method: http://jsfiddle.net/shantanu_k/Lnupw3p2/7/ 我已经为第二种方法创建了jsfiddle: http : //jsfiddle.net/shantanu_k/Lnupw3p2/7/

Updated based on updated question (form has to be invalid if any conditions fail & thus making submit button disabled): http://jsfiddle.net/shantanu_k/Lnupw3p2/10/ 根据更新的问题进行更新(如果任何条件失败,则表单必须无效,从而禁用提交按钮): http : //jsfiddle.net/shantanu_k/Lnupw3p2/10/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM