Javascript AngularJS-验证输入字段的动态模式
基本上,我需要能够注册两种类型的代码。代码类型A只有数字,代码类型B有数字、一个连字符、一个字母值和一个数值 假设情况:Javascript AngularJS-验证输入字段的动态模式,javascript,angularjs,validation,Javascript,Angularjs,Validation,基本上,我需要能够注册两种类型的代码。代码类型A只有数字,代码类型B有数字、一个连字符、一个字母值和一个数值 假设情况: 我注册代码类型A,自定义指令验证所有值 我需要注册代码类型B,我选中“验证代码”选项 类型B',更改模式以进行验证,然后验证输入的所有值 代码-类型A 12345678 32445678 56535678 带补码的代码-类型B 32445678-a1 32445678-a2 65434567-b1 形式 <form> <span>Insert
- 我注册代码类型A,自定义指令验证所有值
- 我需要注册代码类型B,我选中“验证代码”选项 类型B',更改模式以进行验证,然后验证输入的所有值
12345678
32445678
56535678
带补码的代码-类型B
32445678-a1
32445678-a2
65434567-b1
形式
<form>
<span>Insert code:</span><br/>
<input type="text" data-ng-model="code" code-type> <br/>
<input type="checkbox" data-ng-model="validateCodeTypeB" /> Validate code Type B
</form>
在这里,我为您的解决方案制作了plnkr:
if(!scope.codeType){
transformedInput = text.replace(/[^0-9]/g, ''); // For CodeTypeA
} else {
transformedInput = text.replace(/[^a-z]/g,''); // For CodeTypeB, change it to your own regex
}
但是,对于CodetypeB来说,正则表达式是不同的,将它更改为CodetypeB的正则表达式,这样就很好了。您对正则表达式也有问题吗?或者只是发布第二个正则表达式?
if(!scope.codeType){
transformedInput = text.replace(/[^0-9]/g, ''); // For CodeTypeA
} else {
transformedInput = text.replace(/[^a-z]/g,''); // For CodeTypeB, change it to your own regex
}