I'm Lost ....Help. Validation not defined...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DougB
    New Member
    • Nov 2006
    • 14

    I'm Lost ....Help. Validation not defined...

    Hey Gang, aside from mistakenly posting this in the Java Forum first it's my first time here so be gentle. I've been learning javascript through our local college and have run up against a problem. Maybe someone can help.

    I'm using a simple validation script (external js) but continually get the same message "validation is not defined" and I can't get it to work for me.... Here's the code I'm using to access my form:
    Code:
    function validation(document.forms.myForm) { 
    
    if (document.forms.myForm.e-mail.value.indexOf( "@" ) == -1)
    
    {
    alert( "Your e-mail address has been incorrectly entered. Please re-enter it" );
    return false;
    }
    else
    myForm.submit();
    }
    In addition to this, the reference I get when using FF javacript tool is that my first line in the html declaration <?xml version="1.0" encoding="UTF-8"?>

    What am I missing...sigh. Lost in java hell...

    DougB
  • AricC
    Recognized Expert Top Contributor
    • Oct 2006
    • 1885

    #2
    This will validate an email field:
    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>

    Comment

    • AricC
      Recognized Expert Top Contributor
      • Oct 2006
      • 1885

      #3
      I was lazy earlier here is your code changed up a bit for what you are looking for:

      Code:
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[url="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/url]">
      <html xmlns="[url="http://www.w3.org/1999/xhtml"]http://www.w3.org/1999/xhtml[/url]" >
      <head>
      	<title>Untitled Page</title>
      </head>
      <script type="text/javascript">
      function validation(form) 
      {
      var Email = form.Email.value
      if (Email.indexOf( "@" ) == -1)
      {
      alert( "Your e-mail address has been incorrectly entered. Please re-enter it" );
      return false;
      }
      else
      myForm.submit();
      }
      </script>
      <body>
      <form name="myForm" action="testing.htm" method="post">
      <input type="text" name="Email" />
      <input type="button" value="Submit" onclick="validation(this.form)" />
      </form>
       
       
      </body>
      </html>
      HTH,
      Aric

      Comment

      • DougB
        New Member
        • Nov 2006
        • 14

        #4
        Aric,

        Many thanks, I'll try it later on tonight. Much appreciated...

        DougB

        Comment

        • DougB
          New Member
          • Nov 2006
          • 14

          #5
          Hey Aric,

          Still not working (sorry I had to take off for a day or two here and no wireless where I was). Would it help you to know that I'm also using a javascript "reset" function in the form? I'm starting to pull out my hair..haha

          DougB

          Comment

          • DougB
            New Member
            • Nov 2006
            • 14

            #6
            Here's the entire code...

            Code:
            <form name="myForm" id="myForm" action="mailto:me@myPlace.com?subject=Member Sales" method="post" enctype="text/plain">
              
                        <table>
                            <tr>
                              <td colspan="1">Name</td>
                              <td colspan="2"><input name="ID" type="text" /></td>
                            </tr>
                            <tr>
                              <td colspan="1">Address </td>
            		<td colspan="2"><input name="address" type="text" /></td>
            		</tr>
            		<tr>
            		<td colspan="1">City - Province</td>
            		<td  colspan="2"><input name="city-prov" type="text" /></td></tr>
            <tr>
            <td colspan="1">Postal Code</td><td colspan="2"> <input name="postalcode" type="text" /></td>
                            </tr>
                            <tr>
                              <td colspan="1">Home Phone</td><td colspan="2"><input type="text" name="phone-number" /></td></tr><tr>
                              <td colspan="1">E-mail</td><td colspan="2"><input type="text" name="e-mail"  /></td>
                            </tr>
            
                            <tr>
                              <td colspan="1">Item for Sale</td>
                              <td colspan="2">Description: <br /><input name="Sale Item" type="text" value="" /></td>
            		
                            </tr>
                            <tr>
            		<td colspan="1">Price</td>
                              <td colspan="2"><input name="amount" type="text" value="" /></td>
                            </tr>
                            <tr>
                              <td colspan="1">Today's Date (M/D/Y) </td>
                              <td colspan="2"><input name="date" type="text"  /></td>
                            </tr>
                            <tr>
                              <td colspan="3" align="center"><input type="submit" name="Submit" value="Submit" onclick="validation(this.form)" /></td>
                            </tr>
                            <tr>
                              <td colspan="3" align="center" onclick="javascript:document.forms.myForm.reset()"><input type="button" name="clear" value="Reset" /></td>
                            </tr>
                           </table>
                  </form>
            In the <head> section of the html I reference this script with an external javascript

            Code:
            <script type="text/javascript"> src="java/validation.js"></script>function validation(form) 
            {
            var Email = form.Email.value
            if (Email.indexOf( "@" ) == -1)
            {
            alert( "Your e-mail address has been incorrectly entered. Please re-enter it" );
            return false;
            }
            else
            myForm.submit();
            }
            But I still can't get it to work...I'm having a D'oh moment here...please help if you can...

            DougB

            Comment

            • DougB
              New Member
              • Nov 2006
              • 14

              #7
              Anyone....help please

              DougB

              Comment

              • DougB
                New Member
                • Nov 2006
                • 14

                #8
                Greetings Gand,

                I tried resetting the form values to reflect Email instead of E-mail from the form which is calls up the javascript. It didn't make any difference, can anyone help please...

                DougB
                up to my ears in frustration...s igh

                Comment

                • AricC
                  Recognized Expert Top Contributor
                  • Oct 2006
                  • 1885

                  #9
                  The code I posted works for me anyone else test it? Copy and paste it to a new notepad document and name it test.htm then try.


                  Edit:
                  Doug: You have the field names mixed up I renamed the email field from e-mail to Email ( Sorry wrote that from scratch ). On your form you still have e-mail where in your Javascript you are using Email.


                  HTH,
                  Aric

                  Comment

                  • DougB
                    New Member
                    • Nov 2006
                    • 14

                    #10
                    Hoooooeeeeee... .well here's the test page I did (minus the correct mailto: address of course). It allowed the send no problems without valdiation. This is the code I've used and you'll note that the Email called for in the javascript is now what is being sent by the form

                    Code:
                    <html>
                    <head>
                    <title>Test</title>
                    <script type="text/javascript" src="validation.js"></script>
                    </head>
                    <body>
                    <form name="myForm" id="myForm" action="mailto:test@myplace.com?subject=Member Sales" method="post" enctype="text/plain">
                      
                                <table>
                                    <tr>
                                      <td colspan="1">Name</td>
                                      <td colspan="2"><input name="ID" type="text" /></td>
                                    </tr>
                                    <tr>
                                      <td colspan="1">Address </td>
                    		<td colspan="2"><input name="address" type="text" /></td>
                    		</tr>
                    		<tr>
                    		<td colspan="1">City - Province</td>
                    		<td  colspan="2"><input name="city-prov" type="text" /></td></tr>
                    <tr>
                    <td colspan="1">Postal Code</td><td colspan="2"> <input name="postalcode" type="text" /></td>
                                    </tr>
                                    <tr>
                                      <td colspan="1">Home Phone</td><td colspan="2"><input type="text" name="phone-number" /></td></tr><tr>
                                      <td colspan="1">Email</td><td colspan="2"><input type="text" name="email"  /></td>
                                    </tr>
                    
                                    <tr>
                                      <td colspan="1">Item for Sale</td>
                                      <td colspan="2">Description: <br /><input name="Sale Item" type="text" value="" /></td>
                    		
                                    </tr>
                                    <tr>
                    		<td colspan="1">Price</td>
                                      <td colspan="2"><input name="amount" type="text" value="" /></td>
                                    </tr>
                                    <tr>
                                      <td colspan="1">Today's Date (M/D/Y) </td>
                                      <td colspan="2"><input name="date" type="text"  /></td>
                                    </tr>
                                    <tr>
                                      <td colspan="3" align="center"><input type="submit" name="Submit" value="Submit" onclick="validation(this.form)" /></td>
                                    </tr>
                                    <tr>
                                      <td colspan="3" align="center" onclick="javascript:document.forms.myForm.reset()"><input type="button" name="clear" value="Reset" /></td>
                                    </tr>
                                   </table>
                          </form>
                    </body>
                    </html>
                    and here's the javascript file called "validation .js"
                    Code:
                    function validation(form) 
                    {
                    var Email = form.Email.value
                    if (Email.indexOf( "@" ) == -1)
                    {
                    alert( "Your e-mail address has been incorrectly entered. Please re-enter it" );
                    return false;
                    }
                    else
                    myForm.submit();
                    }
                    What am I missing.....hel p meeeeeeeeeeeeee eeeee...Thanks for being persistent!

                    DougB
                    Last edited by DougB; Nov 7 '06, 06:39 PM. Reason: screwed up the code function

                    Comment

                    • AricC
                      Recognized Expert Top Contributor
                      • Oct 2006
                      • 1885

                      #11
                      Code:
                      <td colspan="1">Email</td><td colspan="2"><input type="text" name="email"  /></td>
                      Needs to be

                      Code:
                      <td colspan="1">Email</td><td colspan="2"><input type="text" name="Email"  /></td>
                      Look at the name of your email field and compare it to what you have called in Javascript.

                      Comment

                      • iam_clint
                        Recognized Expert Top Contributor
                        • Jul 2006
                        • 1207

                        #12
                        I think this is what you were trying to achieve.

                        Ok so I didn't use the javascript include method so u will have to split the code how you want it.

                        Code:
                        <script>
                        function validation(form) 
                        {
                        var Email = document.getElementById("email").value
                        if (Email.indexOf( "@" ) == -1)
                        {
                        alert( "Your e-mail address has been incorrectly entered. Please re-enter it" );
                        return false;
                        }
                        else
                        myForm.submit();
                        }
                        </script>
                        <html>
                        <head>
                        <title>Test</title>
                        </head>
                        <body>
                        <form name="myForm" id="myForm" action="mailto:test@myplace.com?subject=Member Sales" method="post" enctype="text/plain">
                          
                                    <table>
                                        <tr>
                                          <td colspan="1">Name</td>
                                          <td colspan="2"><input name="ID" type="text" /></td>
                                        </tr>
                                        <tr>
                                          <td colspan="1">Address </td>
                        		<td colspan="2"><input name="address" type="text" /></td>
                        		</tr>
                        		<tr>
                        		<td colspan="1">City - Province</td>
                        		<td  colspan="2"><input name="city-prov" type="text" /></td></tr>
                        <tr>
                        <td colspan="1">Postal Code</td><td colspan="2"> <input name="postalcode" type="text" /></td>
                                        </tr>
                                        <tr>
                                          <td colspan="1">Home Phone</td><td colspan="2"><input type="text" name="phone-number" /></td></tr><tr>
                                          <td colspan="1">Email</td><td colspan="2"><input type="text" name="email" id="email"  /></td>
                                        </tr>
                        
                                        <tr>
                                          <td colspan="1">Item for Sale</td>
                                          <td colspan="2">Description: <br /><input name="Sale Item" type="text" value="" /></td>
                        		
                                        </tr>
                                        <tr>
                        		<td colspan="1">Price</td>
                                          <td colspan="2"><input name="amount" type="text" value="" /></td>
                                        </tr>
                                        <tr>
                                          <td colspan="1">Today's Date (M/D/Y) </td>
                                          <td colspan="2"><input name="date" type="text"  /></td>
                                        </tr>
                                        <tr>
                                          <td colspan="3" align="center"><input type="button" name="Submit" value="Submit" onclick="validation(this.form)" /></td>
                                        </tr>
                                        <tr>
                                          <td colspan="3" align="center" onclick="javascript:document.forms.myForm.reset()"><input type="button" name="clear" value="Reset" /></td>
                                        </tr>
                                       </table>
                              </form>
                        </body>
                        </html>

                        Comment

                        • DougB
                          New Member
                          • Nov 2006
                          • 14

                          #13
                          iam_clint...

                          many thanks, works like a charm. I also see my original mistake in not calling it from an id too...thanks again! Also note to Aric many thanks as well...
                          Cheers Guys!

                          DougB

                          Comment

                          • AricC
                            Recognized Expert Top Contributor
                            • Oct 2006
                            • 1885

                            #14
                            Glad you finally got it remember most languages are Case Sensitive

                            Comment

                            Working...