gpt4 book ai didi

javascript - 验证 ASP 经典 DDL

转载 作者:行者123 更新时间:2023-11-28 15:45:42 24 4
gpt4 key购买 nike

不幸的是我需要使用 ASP classic...我有一个从数据库加载的下拉列表,但我可以验证它..我需要使用 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是默认值,在这个值下验证需要工作..

希望你能理解我,谢谢

这是对文本框的验证

<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>

最佳答案

我假设您在提交之前有一个需要验证的表单...在下面的示例中,假定表单 id 为“myForm”,提交按钮为

<form id="myForm" ...

实际上是一个输入类型=“button”,onclick为“checkSend();”

<input type="button" onclick="checkSend();" value="submit">

现在选择的 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>

关于javascript - 验证 ASP 经典 DDL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22428655/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com