[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:
<asp:RegularExpressionValidator
ID="REV1"
runat="server"
Display="Static"
ErrorMessage="Error."
ControlToValidate="txtEmailAdd"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" />
but this one returns true
VB.NET Code:
Dim myMatch As Match = System.Text.RegularExpressions.Regex.Match(txtEmailAdd.Text, "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")
If Not myMatch.Success Then
Result.Text = "Error."
Exit Sub
End If
can anyone tell me what's wrong? Thanks in advance.
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
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?
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.
Is not a valid email address. You have two @'s, so the RegularExpressionValidator is correct to fail it.
Gary
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?
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
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
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!!
Re: [RESOLVED] VB.NET regex validation differs from Javascript
Hey,
Out of interest, what was your final implementation?
Gary