Reputation: 391
Unfortinatly I need to use ASP classic... I have a drop down list that I load from database, but i can validate it..I need to do validation with 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 it is the default value, and in this value the validation need to work..
Hope you understand me, Thanks
This is the validation for the 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>
Upvotes: 0
Views: 1466
Reputation: 393
I assume that you have a form to validate before submitting... In the example below, the form id is assumed to be "myForm" and the submit button is
<form id="myForm" ...
actually an input type="button" with an onclick of "checkSend();"
<input type="button" onclick="checkSend();" value="submit">
Also the select now has an Id of "ddlCategories"
The javascript for in the webpage head is as follows:
<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>
So for a demo here's a complete example (without the ASP bit)
<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>
Upvotes: 4