简体   繁体   English

如何在使用Angular的表单中使用正则表达式(无jQuery)

[英]How to use Regular Expressions with a Form using Angular (no jQuery)

I have a simple input field. 我有一个简单的输入字段。 I want to make sure certain rules are met before submission 我想确保提交之前符合某些规则

The password must: 
0. have at least 8 characters
1. have no more than 8 characters
2. have both upper and lower case characters
3. have at least 1 letters
4. have at least 1 digits
5. have one of @ # $
6. contain only characters available on a standard English (US) keyboard. List of valid characters
7. not be an old password

My Html Form 我的HTML表格

<form name="login" action="index_submit" method="get" accept-charset="utf-8">
    <ul>
        <li><label for="username">Email</label>
        <input type="email" name="username" placeholder="username@example.com" required></li>
        <li><label for="password">Current Password</label>
        <input type="password" name="current_password" placeholder="current password" required></li>
        <li><label for="password">New Password</label>
        <input type="password" name="new_password" placeholder="new password" required></li>
        <li>
        <input type="submit" value="Login"></li>
    </ul>
</form>

I only want to apply the rules to the "new_password" 我只想将规则应用于“ new_password”

Add a checkmark (green) if the rules are successful or red X if they don't meet the criteria. 如果规则成功,请添加复选标记(绿色),如果不符合标准,则添加红色X。

I am new to Angular but not new to RegEx. 我是Angular的新手,但不是RegEx的新手。 I have the Expression 我有表达

"^(?=.{8}$)(?=.*[A-Z])(?=.*[0-9])(?=.*[,@#$])"

It would be nice to know which of the rules it has been violated 很高兴知道它违反了哪些规则

You can use ngPattern on the input. 您可以在输入上使用ngPattern。 More information here . 更多信息在这里

You can use ng-pattern for Regex. 您可以将ng-pattern用于Regex。 This will not allow you to know which of the rules encoded in your pattern were violated. 这将使您无法了解违反了模式中编码的哪些规则。 For that, you may want to use a combination of directives like, ng-minlength , ng-maxlength . 为此,您可能需要结合使用ng-minlengthng-maxlength类的指令。

Take a look at: https://docs.angularjs.org/guide/forms 看看: https : //docs.angularjs.org/guide/forms

You can also use custom $validators in order to apply specific validation logic to your fields (but like the other posters mentioned, ng-pattern is great for that purpose). 您也可以使用自定义$ validators,以便将特定的验证逻辑应用于您的字段(但像提到的其他海报一样,ng-pattern对此非常有用)。

It's working in conjunction with models, when you model changes (= the field value changes), it's checking it against the custom registered $validators (through a directive for instance) and updating the model with the results of the validation. 它与模型结合使用,当您对更改进行建模(=字段值更改)时,它将根据自定义注册的$ validators(例如通过指令)对其进行检查,并使用验证结果更新模型。

Here is a working plunkr with your regexp: http://embed.plnkr.co/CdITVLnZOZgAzrqAV4iA/preview (you can use the code view to check out how it's done) 这是使用您的正则表达式的可正常工作的插件: http ://embed.plnkr.co/CdITVLnZOZgAzrqAV4iA/preview(您可以使用代码视图来查看其工作方式)

You can also read the AngularJs documentation about forms, you can do a lot of interesting stuff using directives and models within your forms. 您还可以阅读有关表单的AngularJs文档,您可以在表单中使用指令和模型来做很多有趣的事情。

https://docs.angularjs.org/guide/forms https://docs.angularjs.org/guide/forms

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

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