简体   繁体   中英

How to Validate Email Address using RegularExpression validator?

I have used the below code to validate the email address:

<asp:RegularExpressionValidator ID="EmailAddressFormatValidator" runat="server" 
     ControlToValidate="EmailAddressTextBox" 
     ErrorMessage="RegularExpressionValidator" 
     Display="Dynamic" 
     ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
     EnableClientScript="False">Email Address is not valid.<br />       
</asp:RegularExpressionValidator>

It is working fine, but when users enters " " (space) in the end of the email, it stops working and declare the valid email to invalid, please help me how i can resolve this.

add \\s* towards the end of the regex you have

like this

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

i have set up a demo here

http://regex101.com/r/tX7mA8

the regex I have used here is ^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*\\s*$

I have not much about your language so I suppose those anchor tags are present there for you by default

Just use this:

ValidationExpression="\\s*\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*\\s*" in place of your Validation Expression.

Similar questions can be found here:

http://www.c-sharpcorner.com/forums/

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