0

I want to exclude spaces when validating a textbox in vb.net. Here is the current ValidationExpressopn value:

ValidationExpression="^([a-zA-Z0-9_-.\']+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([a-zA-Z0-9-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$" />

When user inputs space in textbox, I dont want that to render as error.

Example: I include spaces after "1@test.com "

This should not be treater as incorrect data in the textbox.

Any ideas?

JADE
  • 475
  • 4
  • 12
  • 24

1 Answers1

0

If your spaces are leading or trailing you can do a Trim on the expressionToValidate before comparing to your regexp

Dim expressionWithoutTrailingAndLeadingWhiteSpaces As String = originalExpression.Trim()

If you want to modify the regExp to take into account the trailing spaces:

^[_a-z0-9-]+(.[a-z0-9-]+)@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})( *)$

If you want to exclude also leading spaces add an extra ( *) at the beginning of the expression:

^( *)[_a-z0-9-]+(.[a-z0-9-]+)@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})( *)$

Btw - the regExp you are providing is broken - I used the one found here (expression to validate email addresses)

Community
  • 1
  • 1
Ando
  • 11,199
  • 2
  • 30
  • 46