Skip to content

Instantly share code, notes, and snippets.

@peagape
Last active September 14, 2021 03:00
Show Gist options
  • Save peagape/53ce2ae8b167ece0678169f15e53870d to your computer and use it in GitHub Desktop.
Save peagape/53ce2ae8b167ece0678169f15e53870d to your computer and use it in GitHub Desktop.
Um validador de EMAIL, JOSN, BASE64, utilizando REGEX para validar os itens, podendo acrescentar mais itens no Map.
/* //
* Code by Flávio Rocha
* github.com/peagape
* 2021
*
* */
void main()
{
bool hasMatch(String? value, String pattern)
{
return (value == null) ? false :
RegExp(pattern).hasMatch(value);
}
Map<String, dynamic> reGexs = {
'JSON': r'^[{\[]{1}([,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]|".*?")+[}\]]{1}',
'BASE64': r'^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$',
'EMAIL': r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$'
};
//valida Types
bool isValid(String rex,String s)
{
return (s.length < 4) ? false :
hasMatch(s, reGexs[rex]);
}
// Exemplo de como usar:
// isValid (FORMATO, VALUE ) -- retorna true se for valido e false se não estiver correto!
print(isValid('JSON','{"free": "219 MB","total": "1987 MB","usage": "11 %"}')); //retorna TRUE
print(isValid('EMAIL','[email protected]')); //retorna TRUE
print(isValid('BASE64','ISSOnEBASE64@GOOGLE')); //retorna FALSE
print(isValid('EMAIL','flavio.dev1----icloud.com')); //retorna false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment