AngularJS - 删除前导和使用正则表达式从输入盒尾随空白 [英] AngularJS - Remove leading and trailing whitespace from input-box using regex

查看:156
本文介绍了AngularJS - 删除前导和使用正则表达式从输入盒尾随空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个普通的前pression(正则表达式),用于在输入框中添加多个电子邮件ID下列条件:


  1. 多个电子邮件ID必须用逗号隔开

  2. 需要有用于至少一个电子邮件ID

  3. 有不应该在输入字段中的任何空格。

所以我创造了这个正则表达式:

<$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:

  1. Multiple email ids must be separated with comma ,
  2. Need to have atleast one email id
  3. 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 your pattern regex.

See documentation:

ngTrim (optional)

If set to false Angular will not automatically trim the input. This parameter is ignored for input[type=password] controls, which will never trim the input.

(default: true)

这篇关于AngularJS - 删除前导和使用正则表达式从输入盒尾随空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆