Validate textbox value

Posted on 2007-10-02
Last Modified: 2008-04-14

I have a textbox nd a submit button on my .aspx page.

Is there a way i can validate if the value entered in the textbox exist in a table?  I would like to do this before the user clicks on the submit button.

Question by:joan2006
    LVL 6

    Expert Comment

    add some code to your page...with the event onChange on the text box..make the code run a function on the page that:

    opens the database
    runs the following sql

    select * from <Table> where <form field> = <table>.field
    if record set is empty..
    do something.

    close recordset
    close db
    LVL 3

    Expert Comment

    Good fit for an AJAX call.
    onChange invoke an AJAX function to return true/ false from your DB. Then alert the user if it fails.
    PS : The response will be as fast as your DB query here.. SO it better be a simple lookup in DB
    LVL 19

    Expert Comment

    Why not make it a drop down (<select>) instead?  You would populate that drop down with the existing values in your table.  That way, the validation is inherently done for you, since it's now impossible to enter a value that doesn't exist in the db.
    LVL 5

    Expert Comment

    I have done this one php you change this in ASP.NET


          // After enter the value in the textbox called through Ajax
          if(isset($_REQUEST['value'])) {
                $value = $_REQUEST['value'];
                if($value != '') {
                      $valuequery  = "SELECT * FROM users where UserName = '$value'";
                      $valueresult = mysql_query($valuequery);
                      $valuerow = mysql_num_rows($valueresult);

                      // display the correct value for the value
                      if($valuerow > 0) {
                            echo "<input type=text name=test value='' onchange='check_database();'>";
                            echo "<br><font color=red><b>Value is in database</b></font>";
                      else {
                            echo "<input type=text name=test value='$value' onchange='check_database();'>";
                            echo "<br><font color=green><b>Value is not in database</b></font>";
          <!-- Ajax common script it is used in realnigerian copy from that -->
          <script src="scripts/prototype.js" type="text/javascript"></script>
                function checkform() {
                            if(test.value == '') {
                                  alert('Please enter some value');
                                  return false;
                      return false;

                function check_database() {
                      // In which the textbox is display ("id value for the td textbox")
                      var target = 'textvalue';
                      var url = 'validate.php';
                      var params = 'value='+document.validateform.test.value;
                      new Ajax.Updater(target,url,{ parameters:params });
    <form name="validateform" action="validate.php" method="post">
                      <td id="textvalue"><input type="text" name="test" value="" onchange="check_database();"></td>
                      <td><input type="button" name="submit" value="Submit" onclick="return checkform();"></td>

    Accepted Solution

    Thanks guys but i used javascript, onkeypress and attribute.add functionality.  

    Thanks again.
    LVL 1

    Expert Comment

    Closed, 500 points refunded.
    Community Support Moderator

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    In my daily work (mainly using, I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
    This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now