SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Form Validation

  1. #1
    SitePoint Zealot Ap0s7le's Avatar
    Join Date
    Jul 2003
    Location
    Lousiville, KY
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form Validation

    Hey all,

    I have a form that has normal validation, but it's now required to also do a check on the username in real time.

    I have the normal validation in place, now I added a check via ajax using the Prototype library. I have a message popping up, saying the Username is taken, but I don't know how not to have the form return while that message is up.

    Feel free to tell me another way to do it, I don't care. I need it to work, and I'm always willing to learn.

    Thanks,

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Set up your form with a hidden input:
    Code:
    <input type="hidden" name="nameOK" value="yes">
    When your ajax call returns and pops up the message, have it set the value of this hidden input to "no" if the name is taken, else "yes".

    Create or modify your form's onsubmit event handler:
    Code:
    <form name="register" method="post" action="something.php" onsubmit="return checkName();">
    Put the handler in the head of the document:
    Code:
    <script type="text/javascript">
    function checkName() {
      var isNameOK = document.forms["register"].elements["nameOK"].value;
      if (isNameOK == "yes") {
        return true;
      } else {
        alert("You can't submit form, the name is taken");  
        return false;
      }
    }
    </script>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •