문제 설명
Flutter에서 이름을 확인하는 방법은 무엇입니까? (How to validate Name in flutter?)
플러터 프로젝트에서 이름 입력란에 문자, 공백, 점(.)만 허용하고 싶습니다. 공백 5개, 점 3개(.)를 초과할 수 없습니다. 어떻게 할 수 있습니까? 프로그래밍이 처음입니다. 도와주세요.
참조 솔루션
방법 1:
I know there are some geniuses out there who can solve the above problem in Regex. But not everyone is a genius. I always solve these tough regex problems by creating a function for myself that does the same.
Take a look at the function below. It summarizes what I wanted to explain
bool validation(String a){
int spaceNum = 0;
int dotNum = 0;
for (int i = 0; i < a.length; i++){
// check for valid characters
if(!"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .".contains(a[i])){
return false;
}
// check for number of spaces
if (a[i] == " "){
spaceNum += 1;
if (spaceNum > 5){
return false;
}
}
// check for number of dots
if (a[i] == "."){
dotNum += 1;
if (dotNum > 3){
return false;
}
}
}
return true;
}
void main(){
print(validation("Wrong.validation. "));
print(validation("Wrong....validation"));
print(validation("Wrong1Validation12"));
print(validation("Correct Validation..."));
}
The above returns the following results:
false
(because the number of spaces is more than 5)
false
(because the number of dots is more than 3)
false
(because there is a digit instead of a letter)
true
(by xTrinity、CYBERDEVILZ)