|
| 1 | +> [!NOTE] |
| 2 | +> I accept help to make the version of the other programming languages. |
| 3 | +
|
| 4 | +# Multiform-validator |
| 5 | + |
| 6 | +## JAVA >=8 |
| 7 | + |
| 8 | +## How to install |
| 9 | + |
| 10 | +follow the steps below to use the library in your project. |
| 11 | + |
| 12 | +https://jitpack.io/#multiform-validator/java/ |
| 13 | + |
| 14 | +## Available methods - JAVA (0.0.4)v |
| 15 | + |
| 16 | +- CnpjValidator |
| 17 | + - cnpjIsValid |
| 18 | + |
| 19 | +- CpfValidator |
| 20 | + - cpfIsValid |
| 21 | + |
| 22 | +- CreditCardValidator |
| 23 | + - isCreditCardValid |
| 24 | + - identifyCreditCard |
| 25 | + |
| 26 | +- FileValidator |
| 27 | + - isValidAudio |
| 28 | + - file (File) - required |
| 29 | + - audioExtensions (String[]) - default: ["mp3", "wav"] |
| 30 | + - You can exclude the extensions you don't want to validate |
| 31 | + - isValidImage |
| 32 | + - file (File) - required |
| 33 | + - imageExtensions (String[]) - default: ["ico", "jpeg", "png", "gif"] |
| 34 | + - You can exclude the extensions you don't want to validate |
| 35 | + - isValidPdf |
| 36 | + - file (File) - required |
| 37 | + - pdfExtensions (String) - default: "pdf" |
| 38 | + - isValidTxt |
| 39 | + - file (File) - required |
| 40 | + - txtExtensions (String) - default: "txt" |
| 41 | +- Utils |
| 42 | + - getOnlyEmail |
| 43 | + - getOnlyEmailWithOptions (options) |
| 44 | + - multiple (boolean) - default: false |
| 45 | + - cleanDomain (boolean | Arrays<String>) - default: false |
| 46 | + - repeatEmail (boolean) - default: false |
| 47 | + |
| 48 | +- Validate |
| 49 | + - validateEmail |
| 50 | + - maxLength (int) - default: 400 |
| 51 | + - country (String) - default: "" |
| 52 | + - validDomains (boolean) - default: false |
| 53 | + - validDomainsList (String[]) - default: ["@gmail.com", |
| 54 | + "@outlook.com", |
| 55 | + "@yahoo.com", |
| 56 | + "@icloud.com", |
| 57 | + "@hotmail.com", |
| 58 | + "@mail.ru", |
| 59 | + "@yandex.ru", |
| 60 | + "@gmx.com", |
| 61 | + "@zoho.com", |
| 62 | + "@protonmail.com", |
| 63 | + "@protonmail.ch"] |
| 64 | + |
| 65 | +- Validator |
| 66 | + - isAscii |
| 67 | + - isCEP |
| 68 | + - isDate |
| 69 | + - isDecimal |
| 70 | + - isEmail |
| 71 | + - isMACAddress |
| 72 | + - isNumber |
| 73 | + - isPort |
| 74 | + - isPostalCode |
| 75 | + - isTime |
| 76 | + |
| 77 | +## How to use |
| 78 | + |
| 79 | +### CnpjValidator |
| 80 | + |
| 81 | +```java |
| 82 | +// You can import the class or use the full path |
| 83 | + |
| 84 | +import io.github.multiform_validator.CnpjValidator; |
| 85 | + |
| 86 | +public class Main { |
| 87 | + public static void main(String[] args) { |
| 88 | + String cnpjTrue = "69.807.668/0001-41"; |
| 89 | + String cnpjFalse = "61.807.661/0001-48"; |
| 90 | + System.out.println(CnpjValidator.cnpjIsValid(cnpjTrue)); // true |
| 91 | + System.out.println(CnpjValidator.cnpjIsValid(cnpjFalse)); // false |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +### CpfValidator |
| 97 | + |
| 98 | +```java |
| 99 | +// You can also import the method as static or use the full path |
| 100 | + |
| 101 | +import static io.github.multiform_validator.CpfValidator.cpfIsValid; |
| 102 | + |
| 103 | +public class Main { |
| 104 | + public static void main(String[] args) { |
| 105 | + String cpfTrue = "123.456.789-09"; |
| 106 | + String cpfFalse = "123.456.789-10"; |
| 107 | + System.out.println(cpfIsValid(cpfTrue)); // true |
| 108 | + System.out.println(cpfIsValid(cpfFalse)); // false |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +### CreditCardValidator |
| 114 | + |
| 115 | +```java |
| 116 | +import io.github.multiform_validator.CreditCardValidator; |
| 117 | + |
| 118 | +public class Main { |
| 119 | + public static void main(String[] args) { |
| 120 | + String creditCard = "4532 8770 0040 4166"; |
| 121 | + System.out.println(CreditCardValidator.isCreditCardValid(creditCard)); // true |
| 122 | + System.out.println(CreditCardValidator.identifyCreditCard(creditCard)); // Visa |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +### FileValidator |
| 128 | + |
| 129 | +```java |
| 130 | +import io.github.multiform_validator.FileValidator; |
| 131 | + |
| 132 | +import java.io.File; |
| 133 | + |
| 134 | +public class Main { |
| 135 | + public static void main(String[] args) { |
| 136 | + File file = new File("path/to/file"); |
| 137 | + System.out.println(FileValidator.isValidAudio(file)); // true | false |
| 138 | + System.out.println(FileValidator.isValidImage(file)); // true | false |
| 139 | + System.out.println(FileValidator.isValidPdf(file)); // true | false |
| 140 | + System.out.println(FileValidator.isValidTxt(file)); // true | false |
| 141 | + |
| 142 | + exampleExcludingExtensions(); |
| 143 | + } |
| 144 | + |
| 145 | + public static void exampleExcludingExtensions() { |
| 146 | + File file = new File("path/to/file"); |
| 147 | + String[] audioExtensions = {"mp3"}; |
| 148 | + String[] imageExtensions = {"ico", "jpeg", "png"}; |
| 149 | + System.out.println(FileValidator.isValidAudio(file, audioExtensions)); // true | false |
| 150 | + System.out.println(FileValidator.isValidImage(file, imageExtensions)); // false | true |
| 151 | + } |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +### Utils |
| 156 | + |
| 157 | +```java |
| 158 | +import io.github.multiform_validator.Utils; |
| 159 | + |
| 160 | +public class Main { |
| 161 | + public static void main(String[] args) { |
| 162 | + String msg1 = "This is a message example with foo@bar.com email to test"; |
| 163 | + System.out.println(Utils.getOnlyEmail(msg1, null)); // foo@bar.com |
| 164 | + |
| 165 | + String msg2 = "Example two foo1@bar.com and foo2@bar.com"; |
| 166 | + // With options |
| 167 | + Utils.GetOnlyEmailOptionsParams options = new Utils.GetOnlyEmailOptionsParams(); |
| 168 | + options.setMultiple(true); |
| 169 | + System.out.println(Utils.getOnlyEmailWithOptions(msg2, options)); // [foo1@bar.com, foo2@bar.com] |
| 170 | + } |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +### Validate |
| 175 | + |
| 176 | +```java |
| 177 | +import io.github.multiform_validator.Validate; |
| 178 | +import io.github.multiform_validator.Validate.ValidateEmailOptionsParams; |
| 179 | + |
| 180 | +import java.util.Collections; |
| 181 | + |
| 182 | +public class Main { |
| 183 | + public static void main(String[] args) { |
| 184 | + validateEmailExample(); |
| 185 | + } |
| 186 | + |
| 187 | + public static void validateEmailExample() { |
| 188 | + // IMPORTANT: validDomains can not be used with validDomainsList, you can use only one of them |
| 189 | + |
| 190 | + // Basic email validation |
| 191 | + boolean isValid = Validate.validateEmail("example@example.com"); |
| 192 | + System.out.println("Is valid: " + isValid); // Expected: true |
| 193 | + |
| 194 | + // Email validation with options: maxLength |
| 195 | + ValidateEmailOptionsParams optionsMaxLength = new ValidateEmailOptionsParams(); |
| 196 | + optionsMaxLength.setMaxLength(10); // Setting max length to 10, which should fail for longer emails |
| 197 | + boolean isValidMaxLength = Validate.validateEmail("longemail@example.com", optionsMaxLength); |
| 198 | + System.out.println("Is valid with maxLength: " + isValidMaxLength); // Expected: false |
| 199 | + |
| 200 | + // Email validation with options: country specific |
| 201 | + ValidateEmailOptionsParams optionsCountry = new ValidateEmailOptionsParams(); |
| 202 | + optionsCountry.setCountry("us"); // Expecting an email from the US |
| 203 | + boolean isNotValidCountry = Validate.validateEmail("example@domain.com", optionsCountry); |
| 204 | + boolean isValidCountry = Validate.validateEmail("example@domain.com.us", optionsCountry); |
| 205 | + System.out.println("Is not valid with country: " + isNotValidCountry); // Expected: false |
| 206 | + System.out.println("Is valid with country: " + isValidCountry); // Expected: true |
| 207 | + |
| 208 | + // Email validation with options: validDomains |
| 209 | + ValidateEmailOptionsParams optionsValidDomains = new ValidateEmailOptionsParams(); |
| 210 | + optionsValidDomains.setValidDomains(true); // Only allow certain domains (implementation specific) |
| 211 | + boolean isValidValidDomains = Validate.validateEmail("example@gmail.com", optionsValidDomains); |
| 212 | + System.out.println("Is valid with validDomains: " + isValidValidDomains); // Expected: true |
| 213 | + |
| 214 | + // Email validation with options: validDomainsList |
| 215 | + ValidateEmailOptionsParams optionsValidDomainsList = new ValidateEmailOptionsParams(); |
| 216 | + optionsValidDomainsList.setValidDomainsList(Collections.singletonList("specificdomain.com")); // Adding a specific domain to the list |
| 217 | + boolean isValidValidDomainsList = Validate.validateEmail("example@specificdomain.com", optionsValidDomainsList); |
| 218 | + System.out.println("Is valid with validDomainsList: " + isValidValidDomainsList); // Expected: true |
| 219 | + } |
| 220 | +} |
| 221 | +``` |
| 222 | + |
| 223 | +### Validator |
| 224 | + |
| 225 | +```java |
| 226 | +import io.github.multiform_validator.Validator; |
| 227 | + |
| 228 | +public class Main { |
| 229 | + public static void main(String[] args) { |
| 230 | + validMethods(); |
| 231 | + invalidMethods(); |
| 232 | + } |
| 233 | + |
| 234 | + public void validMethods() { |
| 235 | + System.out.println(Validator.isAscii("foo")); // true |
| 236 | + System.out.println(Validator.isCEP("12345-678")); // true |
| 237 | + System.out.println(Validator.isDate("2021-01-01")); // true |
| 238 | + System.out.println(Validator.isDecimal("1.5")); // true |
| 239 | + System.out.println(Validator.isMACAddress("00:00:00:00:00:00")); // true |
| 240 | + System.out.println(Validator.isNumber("123")); // true |
| 241 | + System.out.println(Validator.isPort("8080")); // true |
| 242 | + System.out.println(Validator.isPostalCode("12345-678")); // true |
| 243 | + System.out.println(Validator.isTime("12:00")); // true |
| 244 | + System.out.println(Validator.isEmail("foo@bar.com")); // true |
| 245 | + } |
| 246 | + |
| 247 | + public void invalidMethods() { |
| 248 | + System.out.println(Validator.isAscii("こんにちは")); // false |
| 249 | + System.out.println(Validator.isCEP("12345678")); // false |
| 250 | + System.out.println(Validator.isDate("2021-01-32")); // false |
| 251 | + System.out.println(Validator.isDecimal("1.5.5")); // false |
| 252 | + System.out.println(Validator.isMACAddress("00:00:00:00:00:00:00")); // false |
| 253 | + System.out.println(Validator.isNumber("123a")); // false |
| 254 | + System.out.println(Validator.isPort("8080a")); // false |
| 255 | + System.out.println(Validator.isPostalCode("12345678")); // false |
| 256 | + System.out.println(Validator.isTime("12:00:00")); // false |
| 257 | + System.out.println(Validator.isEmail("foo@bar")); // false |
| 258 | + } |
| 259 | +} |
| 260 | +``` |
| 261 | + |
| 262 | +Lib is in development, there's other validators that you can use, but they are not yet documented. |
0 commit comments