Web Analytics Made Easy -
StatCounter Form Validation: - CodingForum

Announcement

Collapse
No announcement yet.

Form Validation:

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Form Validation:

    Hi!

    This is my first attempt at form validation and it's giving me fits. I've tried to copy/paste and modify code from examples around the web, but I can't find any all inclusive code examples, and can't figure out which parts I should change/not change to suit my form. Basically, I'm having trouble following the code. I'm also having trouble figuring out how to call the script when the user clicks the submit button. I'm kind of new to javascript, and my entire site has been in PHP thus far.

    My form is named "Oregistration"

    it has 5 fields:
    fname
    lname
    nickname
    password
    email

    I would like to validate to make sure the e-mail is in the correct format. Also, I'd like to make sure that all fields are not blank and contain no more than 25 characters. That's all, nothing too strict. If anyone can help point me in the right direction with a code example that would be great. Again, with other code examples I have trouble finding which elements to replace with elements specific to my form. Also, I need to know exactly how to call the validate function when the visitor clicks submit.

    Appreciated.

  • #2
    you should

    call the function onsubmit as ask for a return

    <form onsubmit="return validate()">

    In your function, give the desired conditions and, if they are not fulfilled, give an alert and a return false

    very simplified:
    Code:
    function validate(){
    .....
    if(document.myform.myelement.value==''){
    alert('you must fill the "myelement" field!');
    return false
    }
    .....
    }
    return false will prevent the submit action. If the condition is fulfilled, the default return is something (the function itself, in this case), which is equivalent with a Boolean true, so that the HTML submit action will follow it's normal course
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

    Comment


    • #3
      Still working on it, I think if you guys can help me to get just the first field working I'll be able to figure the rest out.. here's what I have:

      Here's the form calling the function in onsubmit:

      Code:
      <form id="Oregistration" onsubmit="return validate()" name="registration" action="processreg.php" method="post"

      Then I tried to do a simple script to just make sure the one field named "fname" is filled out (I placed this between the <head> and </head> tags):

      Code:
      <script type="text/javascript">
      function validate(){
      if(document.Oregistration.fname.value==''){
      alert('you must fill the "First Name" field!');
      return false;
      }
      </script>
      How can I modify the above code to make it work? I think if I can just get one field, I can figure out the rest.

      Thanks again.

      Comment


      • #4
        bump

        still having trouble, can anyone see where i went wrong in the above code?

        Comment


        • #5
          You're missing an end bracket in your function. The below code should fix your problem. Also, I would recommend making your form name and ID the same.

          Code:
          <script type="text/javascript">
          function validate(){
          	if(document.Oregistration.fname.value==''){
          		alert('you must fill the "First Name" field!');
          		return false;
          	}
          }
          </script>
          Sarah

          Comment


          • #6
            Nevermind
            Last edited by ruggeddesign; Sep 27, 2006, 05:52 AM.

            Comment

            Working...
            X