Results 1 to 9 of 9

Thread: [RESOLVED] VB.NET regex validation differs from Javascript

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Location
    Philippines
    Posts
    117

    Resolved [RESOLVED] VB.NET regex validation differs from Javascript

    Given the following codes and my input is new@[email protected]
    this one returns false
    VB.NET Code:
    1. <asp:RegularExpressionValidator
    2.      ID="REV1"
    3.      runat="server"
    4.      Display="Static"
    5.      ErrorMessage="Error."
    6.      ControlToValidate="txtEmailAdd"
    7.      ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" />

    but this one returns true
    VB.NET Code:
    1. Dim myMatch As Match = System.Text.RegularExpressions.Regex.Match(txtEmailAdd.Text, "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")
    2. If Not myMatch.Success Then
    3.     Result.Text = "Error."
    4.     Exit Sub
    5. End If
    can anyone tell me what's wrong? Thanks in advance.
    Last edited by savior14; Apr 27th, 2010 at 05:09 AM.
    im a newbie in vb.net field so please always bear with me

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: VB.NET regex validation differs from Javascript

    Hey,

    The regular expression that you are using is only matching the domain of the email. You can see this if you run a test here:

    http://gskinner.com/RegExr/

    The second one works because you are looking for a "match" within new@[email protected], and it is finding one. However, your RegularExpressionValidator is trying to match the entire string that you give, it, and that isn't what you are testing for.

    Gary

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Location
    Philippines
    Posts
    117

    Re: VB.NET regex validation differs from Javascript

    Thanks for the info. Appreciated it =)

    so what will I change in my RegularExpression to match the validation from what i have in my RegularExpressionValidator?
    im a newbie in vb.net field so please always bear with me

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: VB.NET regex validation differs from Javascript

    Hold on, hold on, hold on....

    The regular expresssion that you have is valid, in a sense, what you are putting into the TextBox is wrong.

    Code:
    new@[email protected]
    Is not a valid email address. You have two @'s, so the RegularExpressionValidator is correct to fail it.

    Gary

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Location
    Philippines
    Posts
    117

    Re: VB.NET regex validation differs from Javascript

    let me revise my question.

    what should I change on my code-behind codes to correct the validation in order for it to be the same as the validation of my RegularExpressionValidator?
    im a newbie in vb.net field so please always bear with me

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: VB.NET regex validation differs from Javascript

    Hey,

    I would change the Regular Expression to match only if the entire regular expression matches, either using a word boundary:

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

    Or anchors:

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

    In your case, I think anchors make the most sense.

    Hope that helps!!

    Gary

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: VB.NET regex validation differs from Javascript

    The codebehind is matching on the bold part:

    nav@[email protected]

    Which is why you're getting bad results.

    You can get a regex for email addresses here:

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


    So for example, try this:

    ^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$



    With the above regex you should get closer to matching properly

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Location
    Philippines
    Posts
    117

    Re: VB.NET regex validation differs from Javascript

    thanks gep13 and mendhak! =)
    its been a great help! ive been able to validate it correctly. thanks thanks!!
    im a newbie in vb.net field so please always bear with me

  9. #9
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] VB.NET regex validation differs from Javascript

    Hey,

    Out of interest, what was your final implementation?

    Gary

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width