简体   繁体   中英

When multi-line text pasted into text input regex does not match the space

When user pastes something like this (from notepad for example):

multi
line@email.com

into input text box, the line break dissapears and it looks like this:

multi line@email.com

But whatever the line break is converted to does not match this regex: '\\s|\\t|\\r|\\n|\\0','i' so this invalid character passes through js validation to the .NET application code I am working on. It is interesting but this text editor does the same transformation, that is why I had to post original sample as code. I would like to find out what the line break got converted to, so I can add a literal to the regex but I don't know how. Many thanks!

Here is the whole snippet:

var invalidChars = new RegExp('(^[.])|[<]|[>]|[(]|[)]|[\]|[,]|[;]|[:]|([.])[.]|\s|\t|\r|\n|\0', 'i');

if (text.match(invalidChars)) {
       return false;
}

Your immediate problem is escaping. You're using a string literal to create the regex, like this:

'(^[.])|[<]|[>]|[(]|[)]|[\]|[,]|[;]|[:]|([.])[.]|\s|\t|\r|\n|\0'

But before it ever reaches the RegExp constructor, the [\\] becomes [] ; \\s becomes s ; \\0 becomes 0 ; and \\t , \\r and \\n are converted to the characters they represent (tab, carriage return and linefeed, respectively). That won't happen if you use a regex literal instead, but you still have to escape the backslash to match a literal backslash.

Your regex is also has way more brackets than it needs. I think this is what you were trying for:

/^\.|\.\.|[<>()\\,;:\s]/

That matches a dot at the beginning, two consecutive dots, or one of several forbidden characters including any whitespace character ( \\s matches any whitespace character, not just a space).

Ok - here it is

vbCrLF

This is what pasted line breaks are converted to. I added (vbCrLF) group and those spaces are now detected. Thanks, Dan1M

http://forums.asp.net/t/1183613.aspx?Multiline+Textbox+Input+not+showing+line+breaks+in+Repeater+Control

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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