Reputation: 1
I'm new to programming and I'm using Dart for a “introduction to programming“ course.
I'd like to write a code to validate if a text has only letter and space. I've found this for validate spaces.
function validate() {
var field = document.getElementById("myField");
if (field.value.replace(/ /g, "").length == 0) {
alert("Please enter some data (not spaces!)");
}
}
Thanks in advance, also I would need to write a string variable with sentences of different lengths and a function that finds the longest sentence. Any pointers?
Upvotes: 0
Views: 1275
Reputation: 2257
[^a-z ] matches anything that is not a lowercase letter or space. caseSensitive: false makes it not match upper case either.
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);
You can use exp.allMatches("string").length to find the number of characters that are not alpha or whitespace. so you can use:
if (exp.allMatches(field.value).length > 0) {
alert("Please enter some data (not spaces!)");
}
Upvotes: 2