AngularJS - 删除前导和使用正则表达式从输入盒尾随空白 [英] AngularJS - Remove leading and trailing whitespace from input-box using regex
问题描述
我写一个普通的前pression(正则表达式),用于在输入框中添加多个电子邮件ID下列条件:
- 多个电子邮件ID必须用逗号隔开
,
- 需要有用于至少一个电子邮件ID
- 有不应该在输入字段中的任何空格。
所以我创造了这个正则表达式:
<$p$p><$c$c>^(([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5}){1,25})+([,.](([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5}){1,25})+)*$我测试了它在regex101.com及其工作就像一个魅力 https://regex101.com/r/bU7rU8/1
但是,当我将其与code集成,它的工作原理,,但未能在开头和结尾的空格
下面是演示链接: http://jsfiddle.net/2G8gA/330/
AngularJS修剪默认输入的,所以你需要使用 NG-修剪=FALSE
中为了通过开头和结尾的空白,在模式
正则表达式。
请参阅文档:
ngTrim (可选)
如果设置为
假
角不会自动修正输入。此参数忽略输入[类型=密码]
控制,这绝不会修正输入。
的(默认:true)的
块引用>I am writing a regular expression(regex) for adding multiple email ids in an input box with following conditions:
- Multiple email ids must be separated with comma
,
- Need to have atleast one email id
- There should not be any whitespaces in input field.
So i created this regex:
^(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([,.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$
I tested it in regex101.com and its working like a charm https://regex101.com/r/bU7rU8/1
But when i integrate it with code, it works, but fails on leading and trailing whitespace.
Here is the demo link: http://jsfiddle.net/2G8gA/330/
解决方案AngularJS trims the input by default, so you need to use
ng-trim="false"
in order to pass leading and trailing whitespace to yourpattern
regex.See documentation:
ngTrim (optional)
If set to
false
Angular will not automatically trim the input. This parameter is ignored forinput[type=password]
controls, which will never trim the input.(default: true)
这篇关于AngularJS - 删除前导和使用正则表达式从输入盒尾随空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!