Regex email address validation fails if Underscore character present - vb.net

I'm very new to Regular Expressions, but I thought it'd be the best way to validate email addresses entered on my form.
My Regex works, except if the email address entered has an underscore character (_) in it.
Because of my lack of experience with regular expressions, I'm not sure where in my pattern I'm supposed to add the offending character to allow it:
Dim pattern As String = "^[-a-zAZ0-9][-.a-zA-Z0-9]*#[-.a-zA-Z0-0]+(\.[-.a-zA-Z0-0+)*\." & _
"(com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|[a-zA-Z]{2})$"
Another guy on DreamInCode had the same problem. He said he fixed it by adding the _ after the numeric check.
I see the A-Z0-9, but I'm not sure which is the numeric check... I haven't worked much in Regex so I hope nobody minds pointing out where to add the _
Thanks in advance

If you really want a possible fix to your regexp:
Dim pattern As String = "^[a-zA-Z0-9][-\._a-zA-Z0-9]*#[a-zA-Z0-9][-\.a-zA-Z0-9]*\.(com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|[a-zA-Z]{2})$"
Anyway as explained in my comment this still validates ok for many wrong mails.

The only way to validate an email is to send a message at this address and wait for a reply.
A simple check can be to test if there is an # in it. But if you want to use your regex, you have to add the _ within the char class : [-a-zA-Z0-9_]

This is the one which prevents an underscore before the '#' if someone needs this,
^[a-zA-Z0-9][-\.a-zA-Z0-9]*#[a-zA-Z0-9][-\.a-zA-Z0-9]*\.(com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|[a-zA-Z]{2})$
but i have seen some mail address with an underscore in it..as said by M42 is send an email and wait for them to activate their account would be a good choice..but wont be instantly done..

Related

How to make a Password Validator in Scratch

So I am trying to make a password validator in Scratch where it asks the user to input an answer and then it puts the answer through some criterias and then outputs if it is a valid password or not. The criterias are:
Has at least 8 characters,
Has at least one uppercase letter,
Has at least one lowercase letter,
Has at least one number,
Has at least one special character,
Must contain less than 18 characters.
I tried to make a list first with all the different characters and check if the password contained them, but it doesn't actually work. I looked all over the internet for help on this but no one seems to have done it. The Scratch Wiki does have some stuff about case sensitivity but I haven't really been able to implement it. I really need help and I have been trying for a while now. Thanks.
If you just check if the password contains the list, it will only work if it has every single character of the list in order. If you want to make sure it contains each check, you're probably going to have to make a system that checks each letter for every check, which is a little complex.
Check if <lowercase letter/whatever check> contains(letter(text reading #) of (password))
If it passes this check, continue to the next check and set text reading # to 1. Otherwise, change text reading # by 1.
I assume you'll know how to code this properly, but I just partially phrased in the way a normal human would.
This will repeat until either it reaches the end of the password or it passes the check. it will then do this again, but for a different check. It's hard to explain in text, and this is my first answer, but I hope it helps.
You have to use the operators "contains", "length of" and > operators, from the end of the class. Combine "contains", "or" and "and".

VB.NET hardcoding with obfuscation trouble

I need to hardcode a string in my source code and make an "if clause check" to see if the user entered input is the same or not. I am also using EAZfuscator.net for obfuscating the code.
However, when ı use the obfuscator, I think it changes my hardcoded string and I can not perfrom the check. How can I overcome this problem? Can I use HEX values instead. How can I find the HEX values for string characters? I use webcomverters but can not seem to get the correct characters.
Thanks for all the help!

substring and the indexOf method

My assignment in Visual Basic 2010 is to build a order form that has two text boxes, one for the name and the other for the address.
And we're suppose to use the IndexOf method on the address.
I understand that IndexOf returns the position of a character and the number of characters.
What would be the purpose of using the IndexOf method on the address in this instance?
I don't understand what I would be searching for when the user types in it's address that's going to be numbers and string characters.
I think I understand what the IndexOf method does, somewhat, but why and what would I use it to achieve?
You'd typically use IndexOf to -
See if a string contained something
If someString.IndexOf("Avenue") > - 1 Then
'do something
End If
Get the start position of a value in a string , this could then be used to extract part of the string. e.g. someString.Substring(someString.IndexOf("#"),10) would get then next ten characters starting from where the "#" character was found in your string.
Bear in mind you'll always need to handle scenarios where IndexOf will return -1 if it does not find the string your searching for, so your code will have to handle that eventuality.
Since this is a homework question, I will answer with a question or several:
How would you normally enter a full address into a text box?
How would each part of the address be distinguishable from another?
Once you figure out how this can be done, think about IndexOf again.

How can I validate text box input?

I am creating a program and I need to validate my text boxes. For the program the user needs to put in a phrase. But I am not sure how to make sure that the user actually entered in a phrase, the phrase isn't (ex.) skldkfdl, or that there isn't a space.
Strings in Java
You could do a String.Trim() to get rid of trailing whitespaces first...
then do a String.IndexOf(" ") to check for a space.
If the function returns -1, it means there is no space in the string.
Running on the assumption that you're using VB.Net - Add an event handler for the event where you want to validate the text, such as when a "Submit" button is clicked. You may want to use a CancelEventHandler, so that you can cancel the click.
In the event handler, if you're looking for just simple validation, you can use if-statements to check some simple conditions, such as if you just want to check "if input.equals(password)".
Look here for an example of using CancelEventHandler
If you're looking for some more complex validation, you'll want to use regular expressions.
This page might help get you started
Checking to see if something is "a phrase", as in, proper English, would be very difficult. You would need to make sure that all of the words are in the dictionary, and then you would need to check for proper grammar, which is incredibly complex, given English grammar rules. You may want to simplify your approach, depending on your problem. For example, maybe just check that no weird characters are used, that there is more than one space, and that each word contains a vowel.

Validating e-mail with regular expression VB.Net

I'm working on a small project in VB.Net where I get a input from a textbox, and need to verify that this is an e-email address.
I found this expression "^[_a-z0-9-]+(.[_a-z0-9-]+)#[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,4})$", but i cant find any way to test if it passes.
I want some code like:
if not txtEmail.text = regexString then
something happens..
else
something else happens..
end if
Use the System.Text.RegularExpressions.Regex class:
Function IsEmail(Byval email as string) as boolean
Static emailExpression As New Regex("^[_a-z0-9-]+(.[a-z0-9-]+)#[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$")
return emailExpression.IsMatch(email)
End Function
The most important thing to understand about this answer is that I didn't write the regular expression myself. There are just so many wrong ways that seem to be right, and there are several levels of detail that you could take this to. For example, do you want to restrict this to valid top level domains, and if so, how are you accounting for the fact that they are now occasionally adding new TLDs? If the regular expression the most appropriate place for that test, or should have separate code for that check? Even the expression in this answer is now very stale since it was originally authored.
I recommend finding an outside resource for the expression you know will be maintained over time.
Pick your favorite regex from my article on matching email addresses with a regex, and plug it into this Visual Basic code:
If Regex.IsMatch(SubjectString, "regex") Then
Error = False
Else
Error = True
End If
The best regex to match an email address is a controversial topic that I don't want to get into here. My article discusses the issues that you should be aware of when picking a regex. The regex in Joel Coehoorn's answer is definitely not a good one.
There is a great website for this kind of thing, http://regexlib.com/. Not only does it have a tester application where you can paste in a regular expression and test it, but there is also a library of regular expressions you can use with community feedback on their validity, etc. I'm not a regex guru, so I go here when I need a quick regular expression.
Also, if you are thinking of developing regular expressions yourself, there is an excellent tool called Regex Buddy that will allow you to create and test your regular expressions on the fly using an easy to understand English interpretation of your regex.
Possibly off-topic since it's not a regex solution, but you could just use some of the built in features of .NET 2.0:
try
{
MailAddress email = new MailAddress(txtEmail.Text);
}
catch(FormatException fe)
{
// output error
}
That regex isn't really complete... in fact... most aren't (check out this article, or this one).
Unless you really enjoy pain, regex isn't the right way to validate an email address.
Email address: RFC 2822 (simplified)
Matches a normal email address. Does not check the top-level domain.
Requires the "case insensitive" option to be ON.
Dim FoundMatch As Boolean
Try
FoundMatch = Regex.IsMatch(txtEmail.text, "\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase)
Catch ex As ArgumentException
'Syntax error in the regular expression
End Try
If Not FoundMatch Then
Error = True
Else
Error = False
End If