By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,971 Members | 900 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,971 IT Pros & Developers. It's quick & easy.

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

P: 14
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:
Expand|Select|Wrap|Line Numbers
  1. function validation(document.forms.myForm) { 
  2.  
  3. if (document.forms.myForm.e-mail.value.indexOf( "@" ) == -1)
  4.  
  5. {
  6. alert( "Your e-mail address has been incorrectly entered. Please re-enter it" );
  7. return false;
  8. }
  9. else
  10. myForm.submit();
  11. }
  12.  
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
Nov 3 '06 #1
Share this Question
Share on Google+
13 Replies


Expert 100+
P: 1,892
This will validate an email field:
Expand|Select|Wrap|Line Numbers
  1. <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>
Nov 4 '06 #2

Expert 100+
P: 1,892
I was lazy earlier here is your code changed up a bit for what you are looking for:

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head>
  4.     <title>Untitled Page</title>
  5. </head>
  6. <script type="text/javascript">
  7. function validation(form) 
  8. {
  9. var Email = form.Email.value
  10. if (Email.indexOf( "@" ) == -1)
  11. {
  12. alert( "Your e-mail address has been incorrectly entered. Please re-enter it" );
  13. return false;
  14. }
  15. else
  16. myForm.submit();
  17. }
  18. </script>
  19. <body>
  20. <form name="myForm" action="testing.htm" method="post">
  21. <input type="text" name="Email" />
  22. <input type="button" value="Submit" onclick="validation(this.form)" />
  23. </form>
  24.  
  25.  
  26. </body>
  27. </html>
HTH,
Aric
Nov 4 '06 #3

P: 14
Aric,

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

DougB
Nov 4 '06 #4

P: 14
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
Nov 5 '06 #5

P: 14
Here's the entire code...

Expand|Select|Wrap|Line Numbers
  1. <form name="myForm" id="myForm" action="mailto:me@myPlace.com?subject=Member Sales" method="post" enctype="text/plain">
  2.  
  3.             <table>
  4.                 <tr>
  5.                   <td colspan="1">Name</td>
  6.                   <td colspan="2"><input name="ID" type="text" /></td>
  7.                 </tr>
  8.                 <tr>
  9.                   <td colspan="1">Address </td>
  10.         <td colspan="2"><input name="address" type="text" /></td>
  11.         </tr>
  12.         <tr>
  13.         <td colspan="1">City - Province</td>
  14.         <td  colspan="2"><input name="city-prov" type="text" /></td></tr>
  15. <tr>
  16. <td colspan="1">Postal Code</td><td colspan="2"> <input name="postalcode" type="text" /></td>
  17.                 </tr>
  18.                 <tr>
  19.                   <td colspan="1">Home Phone</td><td colspan="2"><input type="text" name="phone-number" /></td></tr><tr>
  20.                   <td colspan="1">E-mail</td><td colspan="2"><input type="text" name="e-mail"  /></td>
  21.                 </tr>
  22.  
  23.                 <tr>
  24.                   <td colspan="1">Item for Sale</td>
  25.                   <td colspan="2">Description: <br /><input name="Sale Item" type="text" value="" /></td>
  26.  
  27.                 </tr>
  28.                 <tr>
  29.         <td colspan="1">Price</td>
  30.                   <td colspan="2"><input name="amount" type="text" value="" /></td>
  31.                 </tr>
  32.                 <tr>
  33.                   <td colspan="1">Today's Date (M/D/Y) </td>
  34.                   <td colspan="2"><input name="date" type="text"  /></td>
  35.                 </tr>
  36.                 <tr>
  37.                   <td colspan="3" align="center"><input type="submit" name="Submit" value="Submit" onclick="validation(this.form)" /></td>
  38.                 </tr>
  39.                 <tr>
  40.                   <td colspan="3" align="center" onclick="javascript:document.forms.myForm.reset()"><input type="button" name="clear" value="Reset" /></td>
  41.                 </tr>
  42.                </table>
  43.       </form>
  44.  
In the <head> section of the html I reference this script with an external javascript

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript"> src="java/validation.js"></script>function validation(form) 
  2. {
  3. var Email = form.Email.value
  4. if (Email.indexOf( "@" ) == -1)
  5. {
  6. alert( "Your e-mail address has been incorrectly entered. Please re-enter it" );
  7. return false;
  8. }
  9. else
  10. myForm.submit();
  11. }
  12.  
But I still can't get it to work...I'm having a D'oh moment here...please help if you can...

DougB
Nov 6 '06 #6

P: 14
Anyone....help please

DougB
Nov 6 '06 #7

P: 14
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...sigh
Nov 7 '06 #8

Expert 100+
P: 1,892
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
Nov 7 '06 #9

P: 14
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. <html>
  3. <head>
  4. <title>Test</title>
  5. <script type="text/javascript" src="validation.js"></script>
  6. </head>
  7. <body>
  8. <form name="myForm" id="myForm" action="mailto:test@myplace.com?subject=Member Sales" method="post" enctype="text/plain">
  9.  
  10.             <table>
  11.                 <tr>
  12.                   <td colspan="1">Name</td>
  13.                   <td colspan="2"><input name="ID" type="text" /></td>
  14.                 </tr>
  15.                 <tr>
  16.                   <td colspan="1">Address </td>
  17.         <td colspan="2"><input name="address" type="text" /></td>
  18.         </tr>
  19.         <tr>
  20.         <td colspan="1">City - Province</td>
  21.         <td  colspan="2"><input name="city-prov" type="text" /></td></tr>
  22. <tr>
  23. <td colspan="1">Postal Code</td><td colspan="2"> <input name="postalcode" type="text" /></td>
  24.                 </tr>
  25.                 <tr>
  26.                   <td colspan="1">Home Phone</td><td colspan="2"><input type="text" name="phone-number" /></td></tr><tr>
  27.                   <td colspan="1">Email</td><td colspan="2"><input type="text" name="email"  /></td>
  28.                 </tr>
  29.  
  30.                 <tr>
  31.                   <td colspan="1">Item for Sale</td>
  32.                   <td colspan="2">Description: <br /><input name="Sale Item" type="text" value="" /></td>
  33.  
  34.                 </tr>
  35.                 <tr>
  36.         <td colspan="1">Price</td>
  37.                   <td colspan="2"><input name="amount" type="text" value="" /></td>
  38.                 </tr>
  39.                 <tr>
  40.                   <td colspan="1">Today's Date (M/D/Y) </td>
  41.                   <td colspan="2"><input name="date" type="text"  /></td>
  42.                 </tr>
  43.                 <tr>
  44.                   <td colspan="3" align="center"><input type="submit" name="Submit" value="Submit" onclick="validation(this.form)" /></td>
  45.                 </tr>
  46.                 <tr>
  47.                   <td colspan="3" align="center" onclick="javascript:document.forms.myForm.reset()"><input type="button" name="clear" value="Reset" /></td>
  48.                 </tr>
  49.                </table>
  50.       </form>
  51. </body>
  52. </html>
  53.  
and here's the javascript file called "validation.js"
Expand|Select|Wrap|Line Numbers
  1. function validation(form) 
  2. {
  3. var Email = form.Email.value
  4. if (Email.indexOf( "@" ) == -1)
  5. {
  6. alert( "Your e-mail address has been incorrectly entered. Please re-enter it" );
  7. return false;
  8. }
  9. else
  10. myForm.submit();
  11. }
  12.  
What am I missing.....help meeeeeeeeeeeeeeeeeee...Thanks for being persistent!

DougB
Nov 7 '06 #10

Expert 100+
P: 1,892
Expand|Select|Wrap|Line Numbers
  1. <td colspan="1">Email</td><td colspan="2"><input type="text" name="email"  /></td>
Needs to be

Expand|Select|Wrap|Line Numbers
  1. <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.
Nov 7 '06 #11

iam_clint
Expert 100+
P: 1,208
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.

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function validation(form) 
  3. {
  4. var Email = document.getElementById("email").value
  5. if (Email.indexOf( "@" ) == -1)
  6. {
  7. alert( "Your e-mail address has been incorrectly entered. Please re-enter it" );
  8. return false;
  9. }
  10. else
  11. myForm.submit();
  12. }
  13. </script>
  14. <html>
  15. <head>
  16. <title>Test</title>
  17. </head>
  18. <body>
  19. <form name="myForm" id="myForm" action="mailto:test@myplace.com?subject=Member Sales" method="post" enctype="text/plain">
  20.  
  21.             <table>
  22.                 <tr>
  23.                   <td colspan="1">Name</td>
  24.                   <td colspan="2"><input name="ID" type="text" /></td>
  25.                 </tr>
  26.                 <tr>
  27.                   <td colspan="1">Address </td>
  28.         <td colspan="2"><input name="address" type="text" /></td>
  29.         </tr>
  30.         <tr>
  31.         <td colspan="1">City - Province</td>
  32.         <td  colspan="2"><input name="city-prov" type="text" /></td></tr>
  33. <tr>
  34. <td colspan="1">Postal Code</td><td colspan="2"> <input name="postalcode" type="text" /></td>
  35.                 </tr>
  36.                 <tr>
  37.                   <td colspan="1">Home Phone</td><td colspan="2"><input type="text" name="phone-number" /></td></tr><tr>
  38.                   <td colspan="1">Email</td><td colspan="2"><input type="text" name="email" id="email"  /></td>
  39.                 </tr>
  40.  
  41.                 <tr>
  42.                   <td colspan="1">Item for Sale</td>
  43.                   <td colspan="2">Description: <br /><input name="Sale Item" type="text" value="" /></td>
  44.  
  45.                 </tr>
  46.                 <tr>
  47.         <td colspan="1">Price</td>
  48.                   <td colspan="2"><input name="amount" type="text" value="" /></td>
  49.                 </tr>
  50.                 <tr>
  51.                   <td colspan="1">Today's Date (M/D/Y) </td>
  52.                   <td colspan="2"><input name="date" type="text"  /></td>
  53.                 </tr>
  54.                 <tr>
  55.                   <td colspan="3" align="center"><input type="button" name="Submit" value="Submit" onclick="validation(this.form)" /></td>
  56.                 </tr>
  57.                 <tr>
  58.                   <td colspan="3" align="center" onclick="javascript:document.forms.myForm.reset()"><input type="button" name="clear" value="Reset" /></td>
  59.                 </tr>
  60.                </table>
  61.       </form>
  62. </body>
  63. </html>
  64.  
Nov 7 '06 #12

P: 14
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
Nov 7 '06 #13

Expert 100+
P: 1,892
Glad you finally got it remember most languages are Case Sensitive
Nov 8 '06 #14

Post your reply

Sign in to post your reply or Sign up for a free account.