How to validate email, SSN, phone number in Java using Regular expressions.

Regular Expressions offer a concise and powerful search-and-replace mechanism.
They are patterns of characters used to perform search, extract or replace operations on the given text. Regular expressions can also be used to validate that the input conforms to a given format.

For example, we can use Regular Expression to check whether the user input is a valid Social Security number, a valid phone number or a valid email number, etc.
Regular Expressions are supported by many languages. Sun added support for regular expression in Java 1.4 by introducing java.util.regex package. This package provides the necessary classes for using Regular Expressions in a java application. It consists of following three main classes ,

Pattern

Matcher

PatternSyntaxException

The java.util.regex package has several other features for appending, text replacement, and greedy/non-greedy pattern matching. See the JDK Documentation on java.util.regex to learn more about using regular expressions in Java.

Using this package I created a utility class to validate some commonly used data elements. My FieldsValidation class has following methods:

Comments 60

This explanation is very helpful. Very.
One minor question: It seems that the email validation accepts an underscore (“_”). I would have expected the validation to reject the character, because it is not specified in expression.

Haro, the regular expression for email validation has a ‘\w’. \w stands for “word character” and that includes [A-Z], [a-z], [0-9] and the “underscore (_)” character. That is why if you have an “_” in your email address it will pass this validation.
Hope that clears it a bit.

zparacha, I’m sure you’re aware this email validator code doesn’t cope with all aspects of the RFC. So should point out it is not definitive (but then what is?)
Was using similar logic until working with an NHS hospital trust in the UK who happily allow apostrophes in the user part of the address (before @) e.g. kathy.o’malley@xxx.nhs.uk
After some argument needless to say the client won by waving his copy of the RFC around. Can’t imagine what problems these users have in general on the web when entering their email addresses, but I now use the simple .+@.+\\.[a-z]+ because I’m fed up with fighting.

“What about not performing validation at all? (I’m serious) Why do people validate email addresses? Either send a verification mail to the address or live with the fact that people will enter addresses that might be valid according to the RFC but don’t work.”

Forgive me for asking what might be a really easy question (I am new to this type of thing)just wondering how you can allow an empty space on a surname. Apparently this can happen!!!!! for example John Mc Ginty (as opposed to John McGinty)

Currently though it does seem to allow a second name of a three word name if that second name is at least 3 characters long. It wont allow it if is only 2 characters
i.e. John Mac Ginty allowed
but John Mc Ginty not allowed
Any ideas would be greatly appreciated!!! 🙂

My wife and i were very thrilled when Raymond managed to round up his web research with the precious recommendations he was given while using the weblog. It is now and again perplexing to simply possibly be releasing tricks which usually the rest might have been trying to sell. We really understand we now have you to thank for this. Those illustrations you made, the straightforward website navigation, the relationships your site make it possible to promote – it is mostly wonderful, and it is making our son in addition to us understand this topic is thrilling, which is certainly wonderfully vital. Thanks for the whole thing!

Happy to have discover it. Plus I have voiced to so several persons about my outcome of losing 20 pounds. I choose 1 tablet every morning and choose a person at lunch for a double dose, and that i keep getting better results. I also have more energy and even sleep more soundly, which helps me to make better food choices, ditch the fatty mochas in the morning, and sense great all day. This is a great product or service. Daidaihua http://www.slimmingcapsulesbuy.com/original-lida-pills-p-6.html

Speaking of the top level domain name since this plays a large role in picking the right domain name for domain name registration; many experts have noted that before you go for a domain name registration, you should identify your top level domain. The .com is the most common top level domain that is used today, for it stands for commercial domains. Of course there are other types and each has its own uses and functions.`*

Furthermore, it is also recommended that you ask family and associates for a good plumbing service
or company. ” flooded homes, clogged bathrooms, overflowing kitchen sinks and even toilet bowls that flush the wrong side. Regular maintenance and prompt HVAC repair will keep your system working efficiently for long and consequently it would help you reduce your energy bills.

My spouse and I stumbled over here coming from a different website and thought I may as well check things out. I like what I see so now i am following you. Look forward to finding out about your web page repeatedly.

This post is very interesting but it took me a long time to find it in google.
I found it on 20 spot, you should focus on quality backlinks building, it will
help you to rank to google top 10. And i know how to help you, just search in google –
k2 seo tips

http://newitech.pl/inwestycje-w-drogi
Buying a used or new automobile can be quite a difficult approach if you do not know what you are actually performing. By teaching yourself about car purchasing prior to head to the dealer, you possibly can make stuff simpler yourself. The following advice may help the next shopping getaway be more pleasurable.

Generally bring a auto technician alongside when searching for a new vehicle. Auto dealers are popular for offering lemons and you do not desire to be their after that target. Provided you can not get a auto mechanic to think about autos together with you, a minimum of make certain you have him take a look at last choice before you buy it.

Know your limits. Before you start purchasing for your upcoming vehicle or truck, determine what you can afford to pay out, and adhere to it. Don’t overlook to incorporate curiosity about your computations. You can expect to pay all around twenty percent as a down payment at the same time, so be ready.

Well before visiting a dealership, know what kind of motor vehicle you need. Study each one of you alternatives ahead of buying so that you can figure out what works well with your financial budget and household needs. Do your homework to determine exactly how much you must pay to get a prospective car.

Prior to signing any agreement take the time to go through each and every collection, including the small print. If there is anything listed that you just do not recognize, do not indicator before you get an solution that you simply fully grasp. Unsavory salesmen may use a contract to put in several service fees that have been not discussed.

When you maintain the preceding guidance under consideration when that you just go buying a vehicle, you will end up prone to get a good offer. Getting a auto lacks as a frustration. Use the ideas from this report and you can have the car you need at a very good price.