Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 验证文本是否只有字母和空格_String_List_Dart - Fatal编程技术网

String 验证文本是否只有字母和空格

String 验证文本是否只有字母和空格,string,list,dart,String,List,Dart,我是编程新手,正在使用Dart学习“编程入门”课程 我想写一个代码来验证文本是否只有字母和空格。我发现这是用于验证空格的 function validate() { var field = document.getElementById("myField"); if (field.value.replace(/ /g, "").length == 0) { alert("Please enter some data (not spaces!)"); } } 提前感谢,我还

我是编程新手,正在使用Dart学习“编程入门”课程

我想写一个代码来验证文本是否只有字母和空格。我发现这是用于验证空格的

function validate() {
  var field = document.getElementById("myField");
  if (field.value.replace(/ /g, "").length == 0)  {
    alert("Please enter some data (not spaces!)");
  }
}

提前感谢,我还需要编写一个字符串变量,其中包含不同长度的句子和一个查找最长句子的函数。有指针吗?

[^a-z]匹配任何不是小写字母或空格的内容。区分大小写:false也使其与大写字母不匹配

RegExp exp = new RegExp(r"[^a-z ]", caseSensitive: false);

print(exp.allMatches("this is valid").length == 0);
print(exp.allMatches("ThIs Is VaLiD").length == 0);
print(exp.allMatches("Th1s 1s NOT val1d").length == 0);
您可以使用exp.allMatches(“string”).length查找非字母或空格的字符数。因此,您可以使用:

if (exp.allMatches(field.value).length > 0)  {
    alert("Please enter some data (not spaces!)");
}

欢迎来到Stack Overflow!根据您找到的示例代码,到目前为止您尝试过做什么?至于您的第二个问题,这应该是一个与此问题不同的新问题。