PHP/AJAX Form Validation - How to?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Haitashi
    New Member
    • Jun 2007
    • 96

    PHP/AJAX Form Validation - How to?

    I have a multi-page form. The first page asks for an email and password. I want the user to enter their email address & password twice before they can click submit. The post action on this page is the second page of this form.

    I wanted to include a script that would check if both emails and both passwords match on the fly; this is so they can receive a "live" response telling them that the emails don't match before they move on to the password or a warning telling then that the passwords don't match before the "submit" button becomes visible.

    Thanks!! ^_^
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    you may call a validation function onsubmit of your form. when that function returns false the submit is stopped and you may give the hint why ;) ... you only have to compare the values of your inputboxes ... but be aware: you should check that on the server too ... because the script may be modified by the users locally ... so you cannot rely on it in case it is security-relevant ...

    try something and post it here in case you have particular problems ...

    kind regards

    ps: hints are onsubmit of your form, a function that returns true or false, values of your textboxes

    Comment

    • Haitashi
      New Member
      • Jun 2007
      • 96

      #3
      Ok, I see.

      I have something like this so far:
      Code:
      <html>
      <head>
      <script type="text/javascript">
      function validate_email(field,alerttxt)
      {
      with (field)
      {
      apos=value.indexOf("@")
      dotpos=value.lastIndexOf(".")
      if (apos<1||dotpos-apos<2) 
        {alert(alerttxt);return false}
      else {return true}
      }
      }
      
      function validate_form(thisform)
      {
      with (thisform)
      {
      if (validate_email(email,"Not a valid e-mail address!")==false)
        {email.focus();return false}
      }
      }
      </script>
      </head>
      
      <body>
      <form action="submitpage.htm"
      onsubmit="return validate_form(this);"
      method="post">
      Email: <input type="text" name="email" size="30">
      <input type="submit" value="Submit"> 
      </form>
      </body>
      
      </html>
      How can I change the warning so that instead of a warning box, the user just gets some text next to the field with the warning information?

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        create a div in the position you need and set its innerHTML with the text you want to display ...

        you're doing well ;)

        kind regards

        Comment

        Working...