⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utilvalidate.java

📁 短信发送
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    public static boolean isNonnegativeInteger(String s) {        if (isEmpty(s)) return defaultEmptyOK;        try {            int temp = Integer.parseInt(s);            if (temp >= 0) return true;            return false;        } catch (Exception e) {            return false;        }        // return(isSignedInteger(s, secondArg)        // &&((isEmpty(s) && secondArg)  ||(parseInt(s) >= 0) ) );    }    /** Returns true if string s is an integer < 0. */    public static boolean isNegativeInteger(String s) {        if (isEmpty(s)) return defaultEmptyOK;        try {            int temp = Integer.parseInt(s);            if (temp < 0) return true;            return false;        } catch (Exception e) {            return false;        }        // return(isSignedInteger(s, secondArg)        // &&((isEmpty(s) && secondArg)  ||(parseInt(s) < 0) ) );    }    /** Returns true if string s is an integer <= 0. */    public static boolean isNonpositiveInteger(String s) {        if (isEmpty(s)) return defaultEmptyOK;        try {            int temp = Integer.parseInt(s);            if (temp <= 0) return true;            return false;        } catch (Exception e) {            return false;        }        // return(isSignedInteger(s, secondArg)        // &&((isEmpty(s) && secondArg)  ||(parseInt(s) <= 0) ) );    }    /** True if string s is an unsigned floating point(real) number.     *     *  Also returns true for unsigned integers. If you wish     *  to distinguish between integers and floating point numbers,     *  first call isInteger, then call isFloat.     *     *  Does not accept exponential notation.     */    public static boolean isFloat(String s) {        if (isEmpty(s)) return defaultEmptyOK;        boolean seenDecimalPoint = false;        if (s.startsWith(decimalPointDelimiter)) return false;        // Search through string's characters one by one        // until we find a non-numeric character.        // When we do, return false; if we don't, return true.        for (int i = 0; i < s.length(); i++) {            // Check that current character is number.            char c = s.charAt(i);            if (c == decimalPointDelimiter.charAt(0)) {                if (!seenDecimalPoint)                    seenDecimalPoint = true;                else                    return false;            } else {                if (!isDigit(c)) return false;            }        }        // All characters are numbers.        return true;    }    /** True if string s is a signed or unsigned floating point     *  (real) number. First character is allowed to be + or -.     *     *  Also returns true for unsigned integers. If you wish     *  to distinguish between integers and floating point numbers,     *  first call isSignedInteger, then call isSignedFloat.     */    public static boolean isSignedFloat(String s) {        if (isEmpty(s)) return defaultEmptyOK;        try {            float temp = Float.parseFloat(s);            if (temp <= 0) return true;            return false;        } catch (Exception e) {            return false;        }        // int startPos = 0;        // if(isSignedFloat.arguments.length > 1) secondArg = isSignedFloat.arguments[1];        // skip leading + or -        // if((s.charAt(0) == "-") ||(s.charAt(0) == "+") ) startPos = 1;        // return(isFloat(s.substring(startPos, s.length), secondArg))    }    /** True if string s is a signed or unsigned floating point     *  (real) number. First character is allowed to be + or -.     *     *  Also returns true for unsigned integers. If you wish     *  to distinguish between integers and floating point numbers,     *  first call isSignedInteger, then call isSignedFloat.     */    public static boolean isSignedDouble(String s) {        if (isEmpty(s)) return defaultEmptyOK;        try {            double temp = Double.parseDouble(s);            return true;        } catch (Exception e) {            return false;        }    }    /** Returns true if string s is letters only.     *     *  NOTE: This should handle i18n version to support European characters, etc.     *  since it now uses Character.isLetter()     */    public static boolean isAlphabetic(String s) {        if (isEmpty(s)) return defaultEmptyOK;        // Search through string's characters one by one        // until we find a non-alphabetic character.        // When we do, return false; if we don't, return true.        for (int i = 0; i < s.length(); i++) {            // Check that current character is letter.            char c = s.charAt(i);            if (!isLetter(c))                return false;        }        // All characters are letters.        return true;    }    /** Returns true if string s is English letters (A .. Z, a..z) and numbers only.     *     *  NOTE: Need i18n version to support European characters.     *  This could be tricky due to different character     *  sets and orderings for various languages and platforms.     */    public static boolean isAlphanumeric(String s) {        if (isEmpty(s)) return defaultEmptyOK;        // Search through string's characters one by one        // until we find a non-alphanumeric character.        // When we do, return false; if we don't, return true.        for (int i = 0; i < s.length(); i++) {            // Check that current character is number or letter.            char c = s.charAt(i);            if (!isLetterOrDigit(c)) return false;        }        // All characters are numbers or letters.        return true;    }    /* ================== METHODS TO CHECK VARIOUS FIELDS. ==================== */    /** isSSN returns true if string s is a valid U.S. Social Security Number.  Must be 9 digits. */    public static boolean isSSN(String s) {        if (isEmpty(s)) return defaultEmptyOK;        String normalizedSSN = stripCharsInBag(s, SSNDelimiters);        return (isInteger(normalizedSSN) && normalizedSSN.length() == digitsInSocialSecurityNumber);    }    /** isUSPhoneNumber returns true if string s is a valid U.S. Phone Number.  Must be 10 digits. */    public static boolean isUSPhoneNumber(String s) {        if (isEmpty(s)) return defaultEmptyOK;        String normalizedPhone = stripCharsInBag(s, phoneNumberDelimiters);        return (isInteger(normalizedPhone) && normalizedPhone.length() == digitsInUSPhoneNumber);    }    /** isUSPhoneAreaCode returns true if string s is a valid U.S. Phone Area Code.  Must be 3 digits. */    public static boolean isUSPhoneAreaCode(String s) {        if (isEmpty(s)) return defaultEmptyOK;        String normalizedPhone = stripCharsInBag(s, phoneNumberDelimiters);        return (isInteger(normalizedPhone) && normalizedPhone.length() == digitsInUSPhoneAreaCode);    }    /** isUSPhoneMainNumber returns true if string s is a valid U.S. Phone Main Number.  Must be 7 digits. */    public static boolean isUSPhoneMainNumber(String s) {        if (isEmpty(s)) return defaultEmptyOK;        String normalizedPhone = stripCharsInBag(s, phoneNumberDelimiters);        return (isInteger(normalizedPhone) && normalizedPhone.length() == digitsInUSPhoneMainNumber);    }    /** isInternationalPhoneNumber returns true if string s is a valid     *  international phone number.  Must be digits only; any length OK.     *  May be prefixed by + character.     */    public static boolean isInternationalPhoneNumber(String s) {        if (isEmpty(s)) return defaultEmptyOK;        String normalizedPhone = stripCharsInBag(s, phoneNumberDelimiters);        return isPositiveInteger(normalizedPhone);    }    /** isZIPCode returns true if string s is a valid U.S. ZIP code.  Must be 5 or 9 digits only. */    public static boolean isZipCode(String s) {        if (isEmpty(s)) return defaultEmptyOK;        String normalizedZip = stripCharsInBag(s, ZipCodeDelimiters);        return (isInteger(normalizedZip) && ((normalizedZip.length() == digitsInZipCode1) || (normalizedZip.length() == digitsInZipCode2)));    }    /** Returns true if string s is a valid contiguous U.S. Zip code.  Must be 5 or 9 digits only. */    public static boolean isContiguousZipCode(String s) {        boolean retval = false;        if (isZipCode(s)) {            if (isEmpty(s)) retval = defaultEmptyOK;            else {                String normalizedZip = s.substring(0,5);                int iZip = Integer.parseInt(normalizedZip);                if ((iZip >= 96701 && iZip <= 96898) || (iZip >= 99501 && iZip <= 99950)) retval = false;                else retval = true;            }        }        return retval;    }    /** Return true if s is a valid U.S. Postal Code (abbreviation for state). */    public static boolean isStateCode(String s) {        if (isEmpty(s)) return defaultEmptyOK;        return ((USStateCodes.indexOf(s) != -1) && (s.indexOf(USStateCodeDelimiter) == -1));    }    /** Return true if s is a valid contiguous U.S. Postal Code (abbreviation for state). */    public static boolean isContiguousStateCode(String s) {        if (isEmpty(s)) return defaultEmptyOK;        return ((ContiguousUSStateCodes.indexOf(s) != -1) && (s.indexOf(USStateCodeDelimiter) == -1));    }    /** Email address must be of form a@b.c -- in other words:     *  - there must be at least one character before the @     *  - there must be at least one character before and after the .     *  - the characters @ and . are both required     */    public static boolean isEmail(String s) {        if (isEmpty(s)) return defaultEmptyOK;        // is s whitespace?        if (isWhitespace(s)) return false;        // there must be >= 1 character before @, so we        // start looking at character position 1        // (i.e. second character)        int i = 1;        int sLength = s.length();        // look for @        while ((i < sLength) && (s.charAt(i) != '@')) i++;        // there must be at least one character after the .        if ((i >= sLength - 1) || (s.charAt(i) != '@'))            return false;        else            return true;        // DEJ 2001-10-13 Don't look for '.', some valid emails do not have a dot in the domain name        // else i += 2;        // look for .        // while((i < sLength) && (s.charAt(i) != '.')) i++;        // there must be at least one character after the .        // if((i >= sLength - 1) || (s.charAt(i) != '.')) return false;        // else return true;    }    /** isYear returns true if string s is a valid     *  Year number.  Must be 2 or 4 digits only.     *     *  For Year 2000 compliance, you are advised     *  to use 4-digit year numbers everywhere.     */    public static boolean isYear(String s) {        if (isEmpty(s)) return defaultEmptyOK;        if (!isNonnegativeInteger(s)) return false;        return ((s.length() == 2) || (s.length() == 4));    }    /** isIntegerInRange returns true if string s is an integer     *  within the range of integer arguments a and b, inclusive.     */    public static boolean isIntegerInRange(String s, int a, int b) {        if (isEmpty(s)) return defaultEmptyOK;        // Catch non-integer strings to avoid creating a NaN below,        // which isn't available on JavaScript 1.0 for Windows.        if (!isSignedInteger(s)) return false;        // Now, explicitly change the type to integer via parseInt        // so that the comparison code below will work both on        // JavaScript 1.2(which typechecks in equality comparisons)        // and JavaScript 1.1 and before(which doesn't).        int num = Integer.parseInt(s);        return ((num >= a) && (num <= b));    }    /** isMonth returns true if string s is a valid month number between 1 and 12. */    public static boolean isMonth(String s) {        if (isEmpty(s)) return defaultEmptyOK;        return isIntegerInRange(s, 1, 12);    }    /** isDay returns true if string s is a valid day number between 1 and 31. */    public static boolean isDay(String s) {        if (isEmpty(s)) return defaultEmptyOK;        return isIntegerInRange(s, 1, 31);    }    /** Given integer argument year, returns number of days in February of that year. */    public static int daysInFebruary(int year) {        // February has 29 days in any year evenly divisible by four,        // EXCEPT for centurial years which are not also divisible by 400.        return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);    }    /** isHour returns true if string s is a valid number between 0 and 23. */    public static boolean isHour(String s) {        if (isEmpty(s)) return defaultEmptyOK;        return isIntegerInRange(s, 0, 23);    }    /** isMinute returns true if string s is a valid number between 0 and 59. */    public static boolean isMinute(String s) {        if (isEmpty(s)) return defaultEmptyOK;        return isIntegerInRange(s, 0, 59);    }    /** isSecond returns true if string s is a valid number between 0 and 59. */    public static boolean isSecond(String s) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -