Show Whether a Textbox Value Exists in Database Using JavaScript

Note: This program will work only with Internet Explorer.

In this article I explain how to show whetehr a TextBox value exists in the database using JavaScript.

First I created a database "EmpSalary_Info". Then I created a table in this database. 

Query Code

CREATE TABLE [dbo].[EmpSalary_Info](

      [id] [int] NULL,

      [name] [varchar](50) NULL,

      [salary] [int] NULL


Now insert some data into the "EmpSalary_Info" table. Then use the following procedure.

Complete Program



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">



    <script type="text/javascript" >

        function ShowDB()


                var txtid = document.getElementById('txtid').value;

                if (txtid.length != 0)


                    var connection = new ActiveXObject("ADODB.Connection");

                    var connectionstring = "Data Source=.;Initial Catalog=EmpDetail;Persist Security Info=True;User ID=sa;Password=password@123;Provider=SQLOLEDB";


                    var rs = new ActiveXObject("ADODB.Recordset");

                    rs.Open("select * from EmpSalary_Info where EmpId=" + txtid, connection);


                    var span = document.createElement("span");

           = "Blue";

                    span.innerText = "  ID " + "  Name " + "   Salary";


                    while (!rs.eof)


                        var span = document.createElement("span");

               = "Red";

                        span.innerText = "\n " + rs.fields(0) + " |  " + rs.fields(1) + " |  " + rs.fields(2);



                        alert("Employee exit in Database");







                    alert("Please Enter Employee Id In TextBox");




    <style type="text/css">



            height: 264px;





    <div id="show" style="font-size: x-large; font-weight: bold; height: 142px; color: #993333;">

        Employee Details<p style="font-size: medium; color: #000000;">

    Enter Employee Id&nbsp;

    <input id="txtid" type="text" /></p>  



    <input id="ShowRecord" type="button" value="Show" onclick="ShowDB()" style="font-weight: bold" />&nbsp;</p>





Output 1


Enter an employee id and then click on the "Show" button.


Output 2

We are not entering a value into the TextBox and we click on the "Show" button.


For more information, download the attached sample application.