Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

How to return an error with JavaScript when someone enter just space on name field in a array?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use a regular expression to test if a word is just spaces, and report an error.

function megaFriend(str) {
  var wordLength = 0;
  var biggestWord;
  for (var i = 0; i < str.length; i++) {
    if (str[i].match(/^s+$/)) {
      alert("Spaces entered");
      return false;
    }
    if (str[i].length > wordLength) {
      var wordLength = str[i].length;
      biggestWord = str[i];
    }
  }
  return biggestWord;
}

console.log(megaFriend(['Nahid', 'Hassan', 'Ahugoggghs', '                ']));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...