How to align regular expressions before validation?

I have a RegularExpressionValidator on my page that checks email using this

<asp:RegularExpressionValidator ID="valEmailExpression" runat="server" ErrorMessage="Your email address does not appear to be of a valid form. (eg: your.name@yourorganisation.com )" ControlToValidate="txtUsername" EnableClientScript="false" ValidationExpression=**"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"** Display="None"></asp:RegularExpressionValidator> 

This works for things like " hello@hello.com "

but if the user cuts and pastes email messages, sometimes you get things like " hello@hello.com " or " hello@hello.com ".

Is it possible to indicate in a regular expression that I would like to trim spaces before checking email?

+6
source share
3 answers

You can simply add space checks to your regular expression:

 \s*\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\s* 
+5
source

Try the following:

http://www.regular-expressions.info/examples.html

Space trimming

You can easily trim unwanted spaces from the beginning and end of a line or lines in a text file by searching and replacing regular expressions. Find ^ [\ t] + and replace with nothing to remove leading spaces (spaces and tabs). Search for [\ t] + $ to trim trailing spaces. Do both by combining regular expressions in ^ [\ t] + | [\ t] + $. Instead of [\ t], which matches a space or tab, you can expand the character class to [\ t \ r \ n] if you also want to split lines. Or you can use the abbreviated \ s instead.

Hope this helps you.

+1
source

I do not believe that you can trim spaces in the regular expression, although you can first use the trim function in the line: http://msdn.microsoft.com/en-us/library/t97s7bs3(v=vs.80).aspx

0
source

Source: https://habr.com/ru/post/889166/


All Articles