Hi experts, i developed javascript code that connects to ms acess database 2003.
Everything work fine as it can insert,update and delete information from database.
But my problem is that when i try to validate all my input text so as to ensure that my user must fill all the form.the validation is not working.Any help rendered will be appreciated.thanks
the code is here
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"& gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script>
function validateForm()
{
if(document.frm.rank_short.value=="")
{
alert("PLEASE ENTER YOUR SURNAME");
document.frm.rank_short.focus();
return false;
}
else if(document.frm.rank_long.value=="")
{
alert("PLEASE ENTER YOUR FIRSTNAME");
document.frm.rank_long.focus();
return false;
}
else if(document.frm.rank_long1.value=="")
{
alert("YOU FORGOT YOUR middlename");
document.frm.rank_long1.focus();
return false;
}
else if(document.frm.rank_long2.value=="")
{
alert("you forgot your regno");
document.frm.rank_long2.focus();
return false;
}
else if(document.frm.rank_long3.value=="")
{
alert("you forgot your class");
document.frm.rank_long3.focus();
return false;
}
else if(document.frm.rank_long4.value=="")
{
alert("you forgot your contact address");
document.frm.rank_long4.focus();
return false;
}
else if(document.frm.rank_long5.value=="")
{
alert("you forgot your state");
document.frm.rank_long5.focus();
return false;
}
}
</script>
<script type="text/javascript">
<!--
var adOpenDynamic = 2;
var adLockOptimistic = 3;
var conn_str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\root\\Desktop\\orominike\\airlinetable.mdb";
var conn = new ActiveXObject("ADODB.Connection");
conn.open(conn_str, "", "");
function get_selected_value(sel) {
return sel.options[sel.options.selectedIndex].value;
}
function gen_list_options(query, id, val) {
var html = ''
var rs = new ActiveXObject("ADODB.Recordset")
rs.open(query, conn, adOpenDynamic, adLockOptimistic)
if(!rs.bof) {
rs.MoveFirst()
while(!rs.eof) {
html += '<option value="' + rs.fields(id).value + '">' + rs.fields(val).value + '</option>'
rs.MoveNext()
}
}
rs.close()
return html
}
function rs2arr(rs, arr) {
for(var i=0; i!= rs.fields.count; ++i)
arr[rs.fields(i).name] = rs.fields(i).value
}
function arr2rs(arr, rs) {
for(var f in arr)
if(rs.fields(f))
rs.fields(f).value = arr[f]
}
function show_menu() {
var html = ''
html += '<input type="button" value="Rank" onclick="show_rank()">'
document.getElementById("main").innerHTML = html
}
function show_rank() {
// write the HTML for the form
var html = ''
html += '<p id="selector"><select id="rank_list" onchange="get_rank(get_selected_value(this))">'
html += '<option value="0">New rank</option>'
html += gen_list_options("SELECT * FROM seniorregister", "ID", "LASTNAME")
html += '</select></p>'
html += '<FORM NAME="frm" METHOD="post" ACTION="" onSubmit="return validateForm()">'
html += '<fieldset><legend> STUDENT</legend>'
html += ' <TABLE style="background-color: white;" WIDTH="1%">'
html += '<p><span id="shortname_p"><TR><TH width="100%"><font size="3" color="green"> LASTNAME</font></th></span><TD width="50%"><input type="text" id="rank_short" size="50"/></TD></TR></p>'
html += '<p><span id="longname_p"><TR><TH width="50%"><font size="3" color="green"> FIRSTNAME</font></th></span><TD width="50%"><input type="text" id="rank_long" size="50"/></TD></TR></p>'
html += '<p><span id="longname_p"><TR><TH width="50%"><font size="3" color="green"> MIDDLENAME</font></th></span><TD width="50%"><input type="text" id="rank_long1" size="50"/></TD></TR></p>'
html += '<p><span id="longname_p"><TR><TH width="50%"><font size="3" color="green"> REGNO</font></th></span><TD width="50%"><input type="text" id="rank_long2" size="50"/></TD></TR></p>'
html += '<p><span id="longname_p"><TR><TH width="100%"><font size="3" color="green"> CLASS</font></th></span><TD width="50%"><input type="text" id="rank_long3" size="50"/></TD></TR></p>'
html += '<p><span id="longname_p"><TR><TH width="100%"><font size="3" color="green"> ADDRESS</font></th></span><TD width="50%"><input type="text" id="rank_long4" size="50"/></TD></TR></p>'
html += '<p><span id="longname_p"><TR><TH width="100%"><font size="3" color="green"> STATE</font></th></span><TD width="50%"><input type="text" id="rank_long5" size="50"/></TD></TR></p>'
html += '<p id="control_p"><input type="button" value="Update" onclick="update_rank()" />'
html += ' <input type="button" value="Delete" onclick="delete_rank()" />'
html += ' <input type="button" value="Back" onclick="show_menu()" /></p>'
html += ' </table>'
html += '</FIELDSET>'
html += '</FORM>'
document.getElementById("main").innerHTML = html
}
function get_rank(id) {
var data = new Array()
if(id == 0) {
// Show the new guy
data["LASTNAME"] = ""
data["FIRSTNAME"] = ""
data["MIDDLENAME"] = ""
data["REGNO"] = ""
data["CLASS"] = ""
data["ADDRESS"] = ""
data["STATE"] = ""
}
else {
var rs = new ActiveXObject("ADODB.Recordset")
rs.open("SELECT * FROM seniorregister WHERE ID = " + id, conn, adOpenDynamic, adLockOptimistic)
rs2arr(rs, data)
rs.close()
}
// Read the resulting transaction state into the form
document.getElementById("rank_short").value = data["LASTNAME"]
document.getElementById("rank_long").value = data["FIRSTNAME"]
document.getElementById("rank_long1").value = data["MIDDLENAME"]
document.getElementById("rank_long2").value = data["REGNO"]
document.getElementById("rank_long3").value = data["CLASS"]
document.getElementById("rank_long4").value = data["ADDRESS"]
document.getElementById("rank_long5").value = data["STATE"]
}
function delete_rank() {
var id = get_selected_value(document.getElementById("rank_list"))
conn.execute("DELETE FROM seniorregister WHERE ID = " + id)
show_rank()
}
function update_rank() {
var id = get_selected_value(document.getElementById("rank_list"))
// Store form state in array
var data = new Array();
data["LASTNAME"] = document.getElementById("rank_short").value
data["FIRSTNAME"] = document.getElementById("rank_long").value
data["MIDDLENAME"] = document.getElementById("rank_long1").value
data["REGNO"] = document.getElementById("rank_long2").value
data["CLASS"] = document.getElementById("rank_long3").value
data["ADDRESS"] = document.getElementById("rank_long4").value
data["STATE"] = document.getElementById("rank_long5").value
// Build the query, selecting only the current record if it exists
var str_sql = "SELECT * FROM seniorregister"
if(id != 0)
str_sql += " WHERE ID = " + id
// Get the record
var rs = new ActiveXObject("ADODB.Recordset")
rs.open(str_sql, conn, 2, 3)
if(id == 0)
rs.AddNew()
arr2rs(data, rs) // dump the data into the record
rs.Update()
// Refresh the UI
var sel = document.getElementById("rank_list").selectedIndex;
show_rank();
document.getElementById("rank_list").selectedIndex = sel;
get_rank(id);
}
//-->
</script>
<title>JS Client-side ADO example</title>
</head>
<body onload="show_menu()">
<div id="main" />
<h1><marquee>welcome to oracle world </marquee></h1>
</body>
</html>
the code below is what i used for validation. pls you can trace where i already added it in my main code 4 i dont know whether the problem is from where i kept form tag element.
NB this code is working perfect before i add this validation.
function validateForm()
{
if(document.frm.rank_short.value=="")
{
alert("PLEASE ENTER YOUR SURNAME");
document.frm.rank_short.focus();
return false;
}
else if(document.frm.rank_long.value=="")
{
alert("PLEASE ENTER YOUR FIRSTNAME");
document.frm.rank_long.focus();
return false;
}
else if(document.frm.rank_long1.value=="")
{
alert("YOU FORGOT YOUR middlename");
document.frm.rank_long1.focus();
return false;
}
else if(document.frm.rank_long2.value=="")
{
alert("you forgot your regno");
document.frm.rank_long2.focus();
return false;
}
else if(document.frm.rank_long3.value=="")
{
alert("you forgot your class");
document.frm.rank_long3.focus();
return false;
}
else if(document.frm.rank_long4.value=="")
{
alert("you forgot your contact address");
document.frm.rank_long4.focus();
return false;
}
else if(document.frm.rank_long5.value=="")
{
alert("you forgot your state");
document.frm.rank_long5.focus();
return false;
}
}
</script>
html += '<FORM NAME="frm" METHOD="post" ACTION="" onSubmit="return validateForm()">'
html += '</FORM>'
thanks