不幸的是我需要使用ASP经典...... 我有一个从数据库加载的下拉列表,但我可以验证它..我需要用JavaScript进行验证
<select name= "ddlCategories">
<option value="-1"> choose
</option>
<%set con = Server.CreateObject("ADODB.Connection")
con.open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("WebData/DB.mdb") & ";"
set rs = con.Execute("Select * FROM Categories where mode=true")
while not rs.eof%>
<option value="<%=rs("CatID")%>"><%=rs("CategoryName")%></option>
<%rs.movenext
wend
rs.close
set rs= nothing
Con.close%>
</select>
-1这是默认值,在此值中验证需要工作..
希望你理解我,谢谢
这是TextBox的验证
<script type="text/javascript">
function ValidationAddCategory()
{
var cName = document.form.tbCategory.value;
if (cName== "")
{
alert("Please insert category name")
return(false)
}
return ture
}
function ValidationDelCategory() {
var oForm = document.getElementById('admin');
var oTemp, strErrors='';
//check 1
oTemp = document.getElementById('ddlCategories');
if (oTemp.value=='-1') {
strErrors+='- Please select a category\n';
}
//put more checks here for any other validation, add to strErrors
if (strErrors) {
alert('There were errors:\n'+strErrors);
}
}
</script>
答案 0 :(得分:4)
我假设您在提交之前有表格需要验证... 在下面的示例中,表单id被假定为“myForm”,提交按钮是
<form id="myForm" ...
实际上是输入类型=“按钮”,其中点击“checkSend();”
<input type="button" onclick="checkSend();" value="submit">
此外,select现在的ID为“ddlCategories”
网页主页中的javascript为:
<script>
function checkSend() {
var oForm = document.getElementById('myForm');
var oTemp, strErrors='';
//check 1
oTemp = document.getElementById('ddlCategories');
if (oTemp.value=='-1') {
strErrors+='- Please select a category\n';
}
//put more checks here for any other validation, add to strErrors
if (strErrors) {
alert('There were errors:\n'+strErrors);
} else {
oForm.submit();
}
}
</script>
所以这里的演示是一个完整的例子(没有ASP位)
<html>
<head>
<script>
function checkSend() {
var oForm = document.getElementById('myForm');
var oTemp, strErrors='';
//check 1
oTemp = document.getElementById('ddlCategories');
if (oTemp.value=='-1') {
strErrors+='- Please select a category\n';
}
//put more checks here for any other validation, add to strErrors
if (strErrors) {
alert('There were errors:\n'+strErrors);
} else {
alert('Submitting...'); //remove this - its for testing
oForm.submit();
}
}
</script>
</head>
<body>
<form id="myForm">
<select id="ddlCategories" name="ddlCategories">
<option value="-1">Please select a category</option>
<option value="1">Test</option> <!-- To be replaced by ASP Classic generation -->
</select>
<br>
<input type="button" onclick="checkSend();" value="submit">
</form>
</body>
</html>