Example usage for com.liferay.portal.kernel.util Validator isDigit

List of usage examples for com.liferay.portal.kernel.util Validator isDigit

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util Validator isDigit.

Prototype

public static boolean isDigit(String s) 

Source Link

Document

Returns true if the string consists of only digits between 0 and 9 (inclusive).

Usage

From source file:com.amf.registration.portlet.RegistrationPortlet.java

License:Open Source License

protected void validate(String firstName, String lastName, String emailAddress, String username,
        int birthdayMonth, int birthdayDay, int birthdayYear, String password1, String password2,
        String homePhone, String mobilePhone, String address, String address2, String city, String zip,
        String securityQuestion, String securityAnswer, boolean acceptedTou) throws Exception {

    if (!Validator.isAlphanumericName(firstName) || (firstName.length() > 50)) {

        throw new ContactFirstNameException();
    }//w  w w  .j a v a  2s .co  m

    if (!Validator.isAlphanumericName(lastName) || (lastName.length() > 50)) {

        throw new ContactLastNameException();
    }

    if (Validator.isNull(emailAddress) || (emailAddress.length() > 75)) {
        throw new UserEmailAddressException();
    }

    if ((username.length() < 4) || (username.length() > 16) || !Validator.isAlphanumericName(username)) {

        throw new UserScreenNameException();
    }

    Date birthday = PortalUtil.getDate(birthdayMonth, birthdayDay, birthdayYear,
            ContactBirthdayException.class);

    Date now = new Date();

    Calendar calendar = Calendar.getInstance();

    calendar.setTime(now);

    int age = CalendarUtil.getAge(birthday, calendar);

    if (age < 13) {
        throw new ContactBirthdayException();
    }

    validatePassword(password1, password2);

    if (Validator.isNotNull(homePhone)) {
        if (!Validator.isDigit(homePhone) || (homePhone.length() != 10)) {
            throw new PhoneNumberException();
        }
    }

    if (Validator.isNotNull(mobilePhone)) {
        if (!Validator.isDigit(mobilePhone) || (mobilePhone.length() != 10)) {

            throw new PhoneNumberException();
        }
    }

    if (!Validator.isAlphanumericName(address) || (address.length() > 75)) {
        throw new AddressStreetException();
    }

    if (Validator.isNotNull(address2)
            && (!Validator.isAlphanumericName(address2) || (address2.length() > 75))) {

        throw new AddressStreetException();
    }

    if (!Validator.isAlphanumericName(city) || (city.length() > 75)) {
        throw new AddressCityException();
    }

    if (!Validator.isDigit(zip) || (zip.length() != 5)) {
        throw new AddressZipException();
    }

    if (Validator.isNull(securityQuestion) || Validator.isNull(securityAnswer)
            || !Validator.isAlphanumericName(securityAnswer) || (securityAnswer.length() > 75)) {

        throw new UserReminderQueryException();
    }

    if (acceptedTou == false) {
        throw new TermsOfUseException();
    }
}

From source file:com.amf.registration.portlet.RegistrationPortlet.java

License:Open Source License

protected void validatePassword(String password1, String password2) throws UserPasswordException {

    if (!password1.equals(password2)) {
        throw new UserPasswordException(UserPasswordException.PASSWORDS_DO_NOT_MATCH);
    }/*w  w  w .j a  v  a 2s  .  com*/

    boolean hasUpperCase = false;
    boolean hasNumber = false;
    boolean hasSpecialCharater = false;

    for (char c : password1.toCharArray()) {
        if (!hasUpperCase && Character.isUpperCase(c)) {
            hasUpperCase = true;
        }

        if (!hasNumber && Validator.isDigit(c)) {
            hasNumber = true;
        }

        if (!hasSpecialCharater) {
            for (char character : SPECIAL_CHARACTER) {
                if (c == character) {
                    hasSpecialCharater = true;
                }
            }
        }

        if (hasUpperCase && hasNumber && hasSpecialCharater) {
            break;
        }
    }

    if (!(hasUpperCase && hasNumber && hasSpecialCharater) || (password1.length() < 6)) {

        throw new UserPasswordException(UserPasswordException.PASSWORD_INVALID);
    }
}

From source file:com.amf.registration.portlet.SearchPortlet.java

License:Open Source License

public void searchUser(ActionRequest actionRequest, ActionResponse actionResponse) throws AddressZipException {

    String zip = ParamUtil.getString(actionRequest, "zip");

    if (!Validator.isDigit(zip) || (zip.length() != 5)) {
        throw new AddressZipException();
    }//from  w w w . ja  v a  2 s  . c o m

    QName qName = new QName("http://amf.com/searchUser", "zip");

    actionResponse.setEvent(qName, zip);
}

From source file:com.amf.registration.registration.portlet.AMFRegistrationPortlet.java

License:Open Source License

protected void validateAddress(String address1, String address2, String city, String zip)
        throws PortalException {

    if (!Validator.isAlphanumericName(address1) || (address1.length() > 75)) {

        throw new AddressStreetException("address1-is-invalid");
    }//from   www .  ja v a2 s  .  c om

    if (Validator.isNotNull(address2)) {
        if (!Validator.isAlphanumericName(address2) || (address2.length() > 75)) {

            throw new AddressStreetException("address2-is-invalid");
        }
    }

    if (!Validator.isAlphanumericName(city) || (city.length() > 75)) {
        throw new AddressCityException();
    }

    if (!Validator.isDigit(zip) || (zip.length() != 5)) {
        throw new AddressZipException();
    }
}

From source file:com.amf.registration.registration.portlet.AMFRegistrationPortlet.java

License:Open Source License

protected boolean validateHomePhone(String homePhone) throws PortalException {

    if (Validator.isNotNull(homePhone)) {
        if (Validator.isDigit(homePhone) && (homePhone.length() == 10)) {

            return true;
        } else {/*from  w ww .j  ava2 s.c om*/
            throw new PhoneNumberException("the-home-phone-is-invalid");
        }
    }

    return false;
}

From source file:com.amf.registration.registration.portlet.AMFRegistrationPortlet.java

License:Open Source License

protected boolean validateMobilePhone(String mobilePhone) throws PortalException {

    if (Validator.isNotNull(mobilePhone)) {
        if (Validator.isDigit(mobilePhone) && (mobilePhone.length() == 10)) {
            return true;
        } else {/*from  w  w  w.j  a v a 2  s  .c  o m*/
            throw new PhoneNumberException("the-mobile-phone-is-invalid");
        }
    }

    return false;
}

From source file:com.amf.registration.registration.portlet.AMFRegistrationPortlet.java

License:Open Source License

protected void validatePassword(String password) throws PortalException {
    if (password.length() < 6) {
        throw new UserPasswordException(UserPasswordException.PASSWORD_LENGTH);
    }// w  w  w  .j  a va  2  s . c om

    boolean hasUppercase = false;
    boolean hasNumber = false;
    boolean hasSpecialCharacter = false;

    for (char c : password.toCharArray()) {
        if (Character.isUpperCase(c)) {
            hasUppercase = true;
        } else if (Validator.isDigit(c)) {
            hasNumber = true;
        } else {
            for (char specialCharacter : _SPECIAL_CHARACTERS) {
                if (c == specialCharacter) {
                    hasSpecialCharacter = true;
                }
            }
        }

        if (hasUppercase && hasNumber && hasSpecialCharacter) {
            return;
        }
    }

    throw new UserPasswordException(UserPasswordException.PASSWORD_INVALID);
}

From source file:com.amf.registration.search.portlet.SearchPortlet.java

License:Open Source License

protected void Validate(String zip) throws PortalException {
    if (!Validator.isDigit(zip) || (zip.length() != 5)) {
        throw new AddressZipException();
    }//from   www .j  a  v a  2 s.co  m
}

From source file:com.evolveum.liferay.usercreatehook.screenname.CustomScreenNameGenerator.java

License:Apache License

public String generate(long companyId, long userId, String emailAddress) throws Exception {

    String screenName = null;//from   w  w w  . java 2  s .  c  o  m

    if (Validator.isNotNull(emailAddress)) {
        // XXX change 1
        // screenName = StringUtil.extractFirst(emailAddress, CharPool.AT).toLowerCase();
        screenName = emailAddress.toLowerCase();

        for (char c : screenName.toCharArray()) {
            // XXX change 2
            // if (!Validator.isChar(c) && !Validator.isDigit(c) && (c != CharPool.DASH) && (c != CharPool.PERIOD))
            // {
            if (!Validator.isChar(c) && !Validator.isDigit(c) && (c != CharPool.DASH)) {
                // XXX change 3
                // screenName = StringUtil.replace(screenName, c, CharPool.PERIOD);
                screenName = StringUtil.replace(screenName, c, CharPool.DASH);
            }
        }

        if (screenName.equals(DefaultScreenNameValidator.CYRUS)
                || screenName.equals(DefaultScreenNameValidator.POSTFIX)) {

            screenName += StringPool.PERIOD + userId;
        }
    } else {
        screenName = String.valueOf(userId);
    }

    if (!_USERS_SCREEN_NAME_ALLOW_NUMERIC && Validator.isNumber(screenName)) {

        screenName = _NON_NUMERICAL_PREFIX + screenName;
    }

    String[] reservedScreenNames = PrefsPropsUtil.getStringArray(companyId,
            PropsKeys.ADMIN_RESERVED_SCREEN_NAMES, StringPool.NEW_LINE, _ADMIN_RESERVED_SCREEN_NAMES);

    for (String reservedScreenName : reservedScreenNames) {
        if (screenName.equalsIgnoreCase(reservedScreenName)) {
            return getUnusedScreenName(companyId, screenName);
        }
    }

    try {
        UserLocalServiceUtil.getUserByScreenName(companyId, screenName);
    } catch (NoSuchUserException nsue) {
        try {
            GroupLocalServiceUtil.getFriendlyURLGroup(companyId, StringPool.SLASH + screenName);
        } catch (NoSuchGroupException nsge) {
            return screenName;
        }
    }

    return getUnusedScreenName(companyId, screenName);
}

From source file:com.liferay.knowledgebase.service.impl.KBStructureLocalServiceImpl.java

License:Open Source License

protected void validate(String title, List<KBStructureField> kbStructureFields) throws PortalException {

    if (Validator.isNull(title)) {
        throw new KBStructureTitleException();
    }//ww w.j  a v  a 2  s  . c  o m

    List<String> kbStructureFieldNames = new ArrayList<String>();
    List<String> kbStructureFieldLabels = new ArrayList<String>();

    for (KBStructureField kbStructureField : kbStructureFields) {
        if (Validator.isNull(kbStructureField.getName())) {
            throw new KBStructureFieldNameException();
        }

        if (kbStructureFieldNames.contains(kbStructureField.getName())) {
            throw new DuplicateKBStructureFieldNameException();
        }

        for (char c : kbStructureField.getName().toCharArray()) {
            if (!Validator.isChar(c) && !Validator.isDigit(c) && (c != CharPool.DASH)
                    && (c != CharPool.UNDERLINE)) {

                throw new KBStructureFieldNameException();
            }
        }

        kbStructureFieldNames.add(kbStructureField.getName());

        if (Validator.isNull(kbStructureField.getLabel())) {
            throw new KBStructureFieldLabelException();
        }

        if (kbStructureFieldLabels.contains(kbStructureField.getLabel())) {
            throw new DuplicateKBStructureFieldLabelException();
        }

        kbStructureFieldLabels.add(kbStructureField.getLabel());

        List<String> kbStructureOptionLabels = new ArrayList<String>();
        List<String> kbStructureOptionValues = new ArrayList<String>();

        for (KBStructureOption kbStructureOption : kbStructureField.getKbStructureOptions()) {

            if (Validator.isNull(kbStructureOption.getLabel())) {
                throw new KBStructureOptionLabelException();
            }

            if (kbStructureOptionLabels.contains(kbStructureOption.getLabel())) {

                throw new DuplicateKBStructureOptionLabelException();
            }

            kbStructureOptionLabels.add(kbStructureOption.getLabel());

            if (Validator.isNull(kbStructureOption.getValue())) {
                throw new KBStructureOptionValueException();
            }

            if (kbStructureOptionValues.contains(kbStructureOption.getValue())) {

                throw new DuplicateKBStructureOptionValueException();
            }

            kbStructureOptionValues.add(kbStructureOption.getValue());
        }
    }
}