package top.octopusyan.config; import com.jfoenix.validation.RegexValidator; import com.jfoenix.validation.RequiredFieldValidator; import com.jfoenix.validation.StringLengthValidator; import com.jfoenix.validation.base.ValidatorBase; import top.octopusyan.model.ProxySetupModel; import top.octopusyan.utils.DomainUtil; import java.util.Objects; import java.util.regex.Pattern; /** * @author : octopus yan * @email : octopus_yan@foxmail.com * @description : JFX文本校验 * @create : 2022-4-2 17:03 */ public class TextValidate { // /** // * 账号格式错误 // */ // public static ValidatorBase AccountFail = new ValidatorBase() { // @Override // protected void eval() { // setMessage("账号格式错误"); // hasErrors.set(true); // } // }; /** * 账号不能为空 */ public static RequiredFieldValidator AccoountRequired = new RequiredFieldValidator("账号不能为空!"); /** * 账号格式 */ public static RegexValidator AccoountValidator = new RegexValidator("账号格式有误!"); /** * 密码不能为空 */ public static RequiredFieldValidator PasswordRequired = new RequiredFieldValidator("密码不能为空!"); /** * 邮箱地址格式验证 */ public static RegexValidator EmailFormat = new RegexValidator("邮箱地址格式错误"); /** * 域名为空检查 */ public static RequiredFieldValidator DomainRequired = new RequiredFieldValidator("域名不能为空!"); /** * 端口为空检查 */ public static RequiredFieldValidator PortRequired = new RequiredFieldValidator("端口不能为空"); /** * 端口格式检查 */ public static final RegexValidator PortFormat = new RegexValidator("端口格式错误"); /** * IP为空检查 */ public static RequiredFieldValidator IpRequired = new RequiredFieldValidator("本地IP不能为空"); /** * IP格式检查 */ public static final RegexValidator IpFormat = new RegexValidator("本地IP格式错误"); static { EmailFormat.setRegexPattern("^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$"); AccoountValidator.setRegexPattern("^[a-zA-Z0-9_-]*$"); PortFormat.setRegexPattern("^([0-9]|[1-9]\\d{1,3}|[1-5]\\d{4}|6[0-4]\\d{4}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])$"); IpFormat.setRegexPattern("^(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])$"); } /** * 文本长度校验 *

message > name + "长度有误" */ public static StringLengthValidator getLengthValidator(String name, int length) { return new StringLengthValidator(name + "长度有误", length); } /** * 文本长度校验 *

message > name + "长度有误,应在" + min + "到" + max + "之间" */ public static RegexValidator getLengthValidator(int min, int max, String name) { String message = name + "长度有误,应在" + min + "到" + max + "之间"; RegexValidator validator = new RegexValidator(message); validator.setRegexPattern("[a-zA-Z0-9_-]{" + min + "," + max + "}$"); return validator; } /** * 域名格式检查 */ public static ValidatorBase domainFormatValidator(ProxySetupModel model) { return new ValidatorBase("域名格式错误,支持数字字母下划线") { @Override protected void eval() { if (!DomainUtil.isCustomize(model.get())) { if (DomainUtil.isHttp(model)) { // http / https boolean matches = Pattern.compile("^[a-zA-Z0-9_-]{3,18}$").matcher(model.getDomain()).matches(); hasErrors.set(!matches); } else { // tcp / udp hasErrors.set(false); } } else { boolean matches = Pattern.compile("^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$") .matcher(model.getDomain() + model.getDomainSuffix()).matches(); hasErrors.set(!matches); } } }; } /** * 域名格式检查 */ public static ValidatorBase domainLengthValidator(ProxySetupModel model) { return new RegexValidator("子域名长度不小于3个字符") { @Override protected void eval() { setRegexPattern("^[a-zA-Z0-9_-]{3,18}$"); if (DomainUtil.isHttp(model) && !DomainUtil.isCustomize(model.get())) { super.eval(); } else { hasErrors.set(false); } } }; } /** * 自定义域名解析检查 */ public static ValidatorBase domainAddressValidator(ProxySetupModel model) { return new ValidatorBase("请输入您的域名,并解析至: " + ProxyConfig.getServerIP(model.getServer())) { @Override protected void eval() { if (!DomainUtil.isCustomize(model.get())) hasErrors.set(false); else hasErrors.set( !Objects.equals( ProxyConfig.getServerIP(model.getServer()), DomainUtil.getDomainAddress(model.getDomain()) ) ); } }; } }