accountutil.java

来自「使用WebLogic Platform构建音乐站点」· Java 代码 · 共 43 行

JAVA
43
字号
package music.ejb.db;

import music.shared.*;

/**
 * 工具类,检查Account字段的格式
 */
public class AccountUtil {

    private static final String USERNAME = "[a-zA-Z][a-zA-Z0-9]+";
    private static final String PASSWORD = "[a-zA-Z0-9]+";

    public static void checkAccountFormat(AccountVO account) throws FormatException {
        checkUsernameFormat(account.getUsername());
        checkPasswordFormat(account.getPassword());

        // make the username lower case:
        account.setUsername(account.getUsername().toLowerCase());
    }

    public static void checkPasswordFormat(String password) throws FormatException {
        if(password==null)
            throw new FormatException("Password mustn't be empty.");
        int length = password.length();
        if(length<6 || length>20)
            throw new FormatException("Password must be 6 to 20 characters.");
        if(!password.matches("[a-zA-Z0-9]+"))
            throw new FormatException("Password must be alphabet or decimal or mixed.");
    }

    public static void checkUsernameFormat(String username) throws FormatException {
        // check username:
        if(username==null)
            throw new FormatException("Username mustn't be empty.");
        int length = username.length();
        if(length<3 || length>20)
            throw new FormatException("Username must be 3 to 20 characters.");
        if(!username.matches(USERNAME))
            throw new FormatException("Username must be alphabet or decimals, and begin with an alphabet.");
    }

}

⌨️ 快捷键说明

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