最小字符数文本框
Minimum character count text box
我的代码由于某种原因无法正常工作..这里是:
html:
<input type="text" name="post" maxlength="140" />
和 javascript:
var inpt = document.getElementsByName("post")[0];
// var inputValue=document.getElementById(post).value;
if (inpt.value < 10) {
return false;
alert("Post must be longer than 10 characters.");
} else {
return true;
}
我试过引用和不引用第二行,但都什么也没做。当我取消引用第二行时,我还确保将 inpt.value 更改为 inputValue.length。
将警报放在 return 语句之前。
有2个问题
var inpt = document.getElementsByName("post")[0];
//need to test the length
if (inpt.value.length < 10) {
alert("Post must be longer than 10 characters.");
//return after the alert
return false;
} else {
return true;
}
还要确保脚本在某个事件上被触发
function validate() {
var inpt = document.getElementsByName("post")[0];
//need to test the length
if (inpt.value.length < 10) {
alert("Post must be longer than 10 characters.");
//return after the alert
return false;
} else {
return true;
}
}
<form onsubmit="return validate()">
<input type="text" name="post" maxlength="140" />
<button>Save</button>
</form>
我的代码由于某种原因无法正常工作..这里是:
html:
<input type="text" name="post" maxlength="140" />
和 javascript:
var inpt = document.getElementsByName("post")[0];
// var inputValue=document.getElementById(post).value;
if (inpt.value < 10) {
return false;
alert("Post must be longer than 10 characters.");
} else {
return true;
}
我试过引用和不引用第二行,但都什么也没做。当我取消引用第二行时,我还确保将 inpt.value 更改为 inputValue.length。
将警报放在 return 语句之前。
有2个问题
var inpt = document.getElementsByName("post")[0];
//need to test the length
if (inpt.value.length < 10) {
alert("Post must be longer than 10 characters.");
//return after the alert
return false;
} else {
return true;
}
还要确保脚本在某个事件上被触发
function validate() {
var inpt = document.getElementsByName("post")[0];
//need to test the length
if (inpt.value.length < 10) {
alert("Post must be longer than 10 characters.");
//return after the alert
return false;
} else {
return true;
}
}
<form onsubmit="return validate()">
<input type="text" name="post" maxlength="140" />
<button>Save</button>
</form>