Java Regex Number Validate isNumeric(String text)

Here you can find the source of isNumeric(String text)

Description

Validation method for Number Format (decimal is excluded)

License

Open Source License

Parameter

Parameter Description
text a parameter

Return

Return true if validation ok, otherwise return false.

Declaration

public static boolean isNumeric(String text) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;

import java.util.Map;
import java.util.regex.Pattern;

public class Main {
    /**/* ww  w  .j a  va2 s .  c o  m*/
     * Validation method for Number Format (decimal is excluded)
     *
     * @param text
     * @return Return true if validation ok, otherwise return false.
     */
    public static boolean isNumeric(String text) {
        if (isEmpty(text)) {
            return false;
        }

        Pattern p = Pattern.compile("[0-9]*");
        return p.matcher(text.trim()).matches();
    }

    /**
     * Check whether the Object has value or not.
     *
     * @param aObj
     * @return if the obj is empty
     */
    public static boolean isEmpty(Object aObj) {
        if (aObj instanceof String) {
            return isEmpty((String) aObj);
        } else if (aObj instanceof Long) {
            return isEmpty((Long) aObj);
        } else if (aObj instanceof java.util.Date) {
            return isEmpty((java.util.Date) aObj);
        } else if (aObj instanceof java.util.Collection) {
            return isEmpty((java.util.Collection) aObj);
        } else if (aObj instanceof java.util.Map) {
            return isEmpty((java.util.Map) aObj);
        } else if (aObj != null && aObj.getClass().isArray()) {
            return isEmptyArray(aObj);
        } else {
            return isNull(aObj);
        }
    }

    /**
     * Check whether the String has value or not.
     *
     * @param aStr
     * @return if the string is empty
     */
    public static boolean isEmpty(String aStr) {
        if (aStr == null || aStr.trim().isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Check whether the Long has value or not.
     *
     * @param aLong
     * @return if the Long is null
     */
    public static boolean isEmpty(Long aLong) {
        if (aLong == null) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Check whether a Collection object is empty.
     *
     * @param c: a java.util.Collection object
     * @return if the Map is empty
     */
    public static boolean isEmpty(Collection c) {
        if (c == null || c.size() == 0) {
            return true;
        }
        return false;
    }

    /**
     * Check whether a Map object is empty.
     *
     * @param m: a java.util.Map object
     * @return if the Map is empty
     */
    public static boolean isEmpty(Map m) {
        if (m == null || m.size() == 0) {
            return true;
        }
        return false;
    }

    /**
     * Check whether the Date has value or not.
     *
     * @param aDate
     * @return if the date is null
     */
    public static boolean isEmpty(java.util.Date aDate) {
        if (aDate == null) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Trim the specified String.
     *
     * @param aStr
     * @return the result string,"" return if string is NULL
     */
    public static String trim(String aStr) {
        if (aStr == null) {
            return "";
        } else {
            return aStr.trim();
        }
    }

    /**
     * Check whether the array is empty or not.
     *
     * @param array
     * @return
     */
    private static boolean isEmptyArray(Object array) {
        int length = 0;
        if (array instanceof int[]) {
            length = ((int[]) array).length;
        } else if (array instanceof byte[]) {
            length = ((byte[]) array).length;
        } else if (array instanceof short[]) {
            length = ((short[]) array).length;
        } else if (array instanceof char[]) {
            length = ((char[]) array).length;
        } else if (array instanceof float[]) {
            length = ((float[]) array).length;
        } else if (array instanceof double[]) {
            length = ((double[]) array).length;
        } else if (array instanceof long[]) {
            length = ((long[]) array).length;
        } else if (array instanceof boolean[]) {
            length = ((boolean[]) array).length;
        } else {
            length = ((Object[]) array).length;
        }
        if (length == 0) {
            return true;
        }
        return false;
    }

    /**
     * Check whether the Object is null or not.
     *
     * @param oStr
     * @return if the object is NULL
     */
    public static boolean isNull(Object oStr) {
        if (oStr == null) {
            return true;
        } else {
            return false;
        }
    }
}

Related

  1. isNumeric(String str)
  2. isNumeric(String str)
  3. isNumeric(String str)
  4. isNumeric(String str)
  5. isNumeric(String str)
  6. isNumeric(String value)
  7. isNumericAndCanNull(String src)
  8. isNumericAndCanNull(String src)
  9. isNumString(String txt)