一:TextInputFormatter篇
(1)忽略特殊字符
import 'package:flutter/services.dart';
const _regExp=r"^[\u4E00-\u9FA5A-Za-z0-9_]+$";
//忽略特殊字符
class IgnoreOtherFormatter extends TextInputFormatter{
@override
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
if(newValue.text.length>0){
if(RegExp(_regExp).firstMatch(newValue.text)!=null){
return newValue;
}
return oldValue;
}
return newValue;
}
}
(2)只能輸入數(shù)字和小寫字母
import 'package:flutter/services.dart';
const _regExp=r"^[Za-z0-9_]+$";
class onlyInputNumberAndLowWorkFormatter extends TextInputFormatter{
@override
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
if(newValue.text.length>0){
if(RegExp(_regExp).firstMatch(newValue.text)!=null){
return newValue;
}
return oldValue;
}
return newValue;
}
}
(3)只能輸入數(shù)字和字母
import 'package:flutter/services.dart';
const _regExp=r"^[ZA-ZZa-z0-9_]+$";
class onlyInputNumberAndWorkFormatter extends TextInputFormatter{
@override
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
if(newValue.text.length>0){
if(RegExp(_regExp).firstMatch(newValue.text)!=null){
return newValue;
}
return oldValue;
}
return newValue;
}
}
(4)只能輸入數(shù)字
WhitelistingTextInputFormatter.digitsOnly
(4)長度限制(限制6位)
LengthLimitingTextInputFormatter(6)
(6)限制單行
BlacklistingTextInputFormatter.singleLineFormatter
二:正則
(1)手機(jī)號驗證
static bool isChinaPhoneLegal(String str) {
return RegExp(
r"^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$")
.hasMatch(str);
}
(2)郵箱驗證
static bool isEmail(String str) {
return RegExp(
r"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$")
.hasMatch(str);
}
(3)驗證URL
static bool isUrl(String value) {
return RegExp(
r"^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+")
.hasMatch(value);
}
(4)驗證身份證
static bool isIdCard(String value) {
return RegExp(
r"\d{17}[\d|x]|\d{15}")
.hasMatch(value);
}
(5)驗證中文
static bool isChinese(String value) {
return RegExp(
r"[\u4e00-\u9fa5]")
.hasMatch(value);
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。