<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    thinking

    one platform thousands thinking

    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 regualer 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:

    1. isEmailValid:

    Validate email address using Java regex

    /** isEmailValid: Validate email address using Java reg ex.
    * This method checks if the input string is a valid email address.
    * @param email String. Email address to validate
    * @return boolean: true if email address is valid, false otherwise.
    */


    public static boolean isEmailValid(String email){
    boolean isValid = false;

    /*
    Email format: A valid email address will have following format:

    • [""w"".-]+ : Begins with word characters, (may include periods and hypens).
    • @ : It must have a ‘@’ symbol after initial characters.
    • ([""w""-]+"".)+ : ‘@’ must follow by more alphanumeric characters (may include hypens.).
      This part must also have a “.” to separate domain and subdomain names.
    • [A-Z]{2,4}$ : Must end with two to four alaphabets.
      (This will allow domain names with 2, 3 and 4 characters e.g pa, com, net, wxyz)

    Examples: Following email addresses will pass validation
    abc@xyz.net; ab.c@tx.gov
    */



    //Initialize reg ex for email.

    String expression = “^[""w"".-]+@([""w""-]+"".)+[A-Z]{2,4}$”;
    CharSequence inputStr = email;


    //Make the comparison case-insensitive.


    Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    if(matcher.matches()){
    isValid = true;
    }
    return isValid;
    }

    Update: Read this post for a more thorough Java regular expression to validate email address.

    2. isPhoneNumberValid:

    Validate phone number using Java reg ex.

    /** isPhoneNumberValid: Validate phone number using Java reg ex.
    * This method checks if the input string is a valid phone number.
    * @param email String. Phone number to validate
    * @return boolean: true if phone number is valid, false otherwise.
    */


    public static boolean isPhoneNumberValid(String phoneNumber){
    boolean isValid = false;

    /* Phone Number format: (nnn)nnn-nnnn; nnnnnnnnnn; nnn-nnn-nnnn

    • ^""(? : May start with an option “(” .
    • (""d{3}): Followed by 3 digits.
    • "")? : May have an optional “)”
    • [- ]? : May have an optional “-” after the first 3 digits or after optional ) character.
    • (""d{3}) : Followed by 3 digits.
    • [- ]? : May have another optional “-” after numeric digits.
    • (""d{4})$ : ends with four digits.

    Examples: Matches following phone numbers:
    (123)456-7890, 123-456-7890, 1234567890, (123)-456-7890

    //Initialize reg ex for phone number.
    String expression = “^""(?(""d{3})"")?[- ]?(""d{3})[- ]?(""d{4})$”;
    CharSequence inputStr = phoneNumber;
    Pattern pattern = Pattern.compile(expression);
    Matcher matcher = pattern.matcher(inputStr);
    if(matcher.matches()){
    isValid = true;
    }
    return isValid;
    }

    3. isValidSSN:

    Validate Social Security Number (SSN) using Java reg ex.

    /** isSSNValid: Validate Social Security number (SSN) using Java reg ex.
    * This method checks if the input string is a valid SSN.
    * @param email String. Social Security number to validate
    * @return boolean: true if social security number is valid, false otherwise.
    */


    public static boolean isSSNValid(String ssn){
    boolean isValid = false;

    /*SSN format xxx-xx-xxxx, xxxxxxxxx, xxx-xxxxxx; xxxxx-xxxx:

    • ^""d{3} : Starts with three numeric digits.
    • [- ]? : Followed by an optional “-”
    • ""d{2} : Two numeric digits after the optional “-”
    • [- ]? : May contain an optional second “-” character.
    • ""d{4} : ends with four numeric digits.

    Examples: 879-89-8989; 869878789 etc.
    */



    //Initialize reg ex for SSN.

    String expression = “^""d{3}[- ]?""d{2}[- ]?""d{4}$”;
    CharSequence inputStr = ssn;
    Pattern pattern = Pattern.compile(expression);
    Matcher matcher = pattern.matcher(inputStr);
    if(matcher.matches()){
    isValid = true;
    }
    return isValid;
    }

    4. isNumeric:

    Validate a number using Java regex.

    /**isNumeric: Validate a number using Java regex.
    * This method checks if the input string contains all numeric characters.
    * @param email String. Number to validate
    * @return boolean: true if the input is all numeric, false otherwise.
    */


    public static boolean isNumeric(String number){
    boolean isValid = false;

    /*Number: A numeric value will have following format:

    • ^[-+]? : Starts with an optional “+” or “-” sign.
    • [0-9]* : May have one or more digits.
    • "".? : May contain an optional “.” (decimal point) character.
    • [0-9]+$ : ends with numeric digit.

    */



    //Initialize reg ex for numeric data.

    String expression = “^[-+]?[0-9]*"".?[0-9]+$”;
    CharSequence inputStr = number;
    Pattern pattern = Pattern.compile(expression);
    Matcher matcher = pattern.matcher(inputStr);
    if(matcher.matches()){
    isValid = true;
    }
    return isValid;
    }

    This example demonstrates how easy it is to validate email address, ssn, phone number in Java using regular expressions. You can read more about regular expression format here .

    You can download complete Java code for this class here .

    Feel free to modify and use this class in your projects. Let me know if you have any questions or comments.

    Enjoy.

    Share and Enjoy:
    • StumbleUpon
    • Digg
    • del.icio.us
    • Reddit
    • Sphinn
    • blinkbits How to validate email, SSN, phone number in Java using Regular expressions.
    • NewsVine
    • Smarking
    • Yahoo! Buzz

    If you enjoyed this post, make sure you subscribe to my RSS feed!

    Related posts:

    Jan 10, 2008
    Filed under: Java
    Tags: , , , , , ,

    12 Responses

    1. haro:

      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.

      Posted on January 23rd, 2008 at 12:29 pm

    2. zparacha:

      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.

      Posted on January 24th, 2008 at 2:41 pm

    3. GDD:

      Really nice article

      Posted on January 27th, 2008 at 1:06 am

    4. Linoleum:

      Regular expressions in Java…

      Confused by regular expressions in Java? This brief tutorial by zparacha may help shed some light on them……

      Posted on January 31st, 2008 at 2:43 am

    5. Deliggit.com | The social sites' most interesting urls:

      How To essentials - Regular expressions in Java | Deliggit.com…

      zparacha.com

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

      This…

    posted on 2009-05-14 16:28 lau 閱讀(1118) 評論(0)  編輯  收藏 所屬分類: J2EE

    主站蜘蛛池模板: 国产精品亚洲mnbav网站| 亚洲ⅴ国产v天堂a无码二区| 亚洲天堂免费在线视频| 亚洲欧洲第一a在线观看| 成人爽A毛片免费看| 2022国内精品免费福利视频| 亚洲欧洲国产精品久久| 又粗又硬免费毛片| 青青草无码免费一二三区| 亚洲AV无码成人精品区狼人影院| 亚洲熟妇无码八AV在线播放 | 国产免费AV片在线播放唯爱网| 亚洲欧洲AV无码专区| 自拍偷自拍亚洲精品被多人伦好爽| 最近高清中文字幕免费| 黄色毛片免费在线观看| 亚洲欧洲国产成人精品| 亚洲午夜国产精品无码| 永久在线毛片免费观看| 日本免费高清视频| 美女视频黄频a免费| 亚洲娇小性xxxx| 国产成人精品日本亚洲网站| 国产精品成人四虎免费视频| 131美女爱做免费毛片| 国产精品免费αv视频| 鲁死你资源站亚洲av| 亚洲天堂在线播放| 国产L精品国产亚洲区久久| 成人午夜18免费看| 免费人成视频在线观看网站| 又粗又长又爽又长黄免费视频| 美女视频黄免费亚洲| 亚洲AV本道一区二区三区四区| 亚洲av高清在线观看一区二区 | 亚洲阿v天堂在线2017免费| 亚洲日本中文字幕天天更新| 中文字幕亚洲综合久久| 亚洲乱码中文字幕综合| 亚洲AV无码一区二区三区在线观看| 欧美a级在线现免费观看|