【发布时间】:2015-06-09 05:19:00
【问题描述】:
我正在寻找一个 textarea 代码,它允许用户仅在他们在提供的 textarea 中写入一定数量的单词(比如 300)时提交表单。如果有人写的字数较少并尝试提交表单,则应显示警告消息。
我根据以下建议尝试了此代码,但即使是三四个字,表单仍会在没有任何警告的情况下提交。请帮忙。
<html>
<form action="articlesuccess.php" method="POST">
<script type="text/javascript">
function checkWordCount(){
s = document.getElementById("article").value;
s = s.replace(/(^\s*)|(\s*$)/gi,"");
s = s.replace(/[ ]{2,}/gi," ");
s = s.replace(/\n /,"\n");
if (s.split(' ').length <= 300) {
alarm("not enough words...");
}
}
</script>
<p>
<label for="article">Write the article in the box below:</label> <br/>
<textarea name="article" id="article" style="width:700px; height:500px;"></textarea>
</p>
<input name="submit" id="submit" type="submit" onclick="checkWordCount()" value="Submit" /><input type="Reset" value="Reset">
</form>
</html>
【问题讨论】: