469,349 Members | 1,492 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,349 developers. It's quick & easy.

Validations not working!!!

41
Hi guys,

I have a problem, can someone help me? I'm not sure why but none of my validation functions are working!!!!




Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  
  3. <head><title>Registration Details</title>
  4.  
  5. <SCRIPT LANGUAGE="JavaScript">
  6.  
  7. function processForm1() {
  8.  
  9. form1 = document.forms["formOne"] 
  10.  
  11.     if (form1.firstname.value==""){
  12.     alert("you must fill in your first name")
  13.  
  14.     formOne.firstname.focus();
  15.  
  16.     }
  17.  
  18.     else if (form1.lastname.value==""){
  19.     alert("you must fill in your lastname")
  20.     formOne.lastname.focus();
  21.     }
  22.  
  23.     else if (form1.postcode.value==""){
  24.     alert("you must fill in your postcode")
  25.     formOne.postcode.focus();
  26.     }
  27.  
  28.     else if (form1.dateofbirth.value==""){
  29.     alert("you must fill in your DOB")
  30.     formOne.dateofbirth.focus();
  31.     }
  32.  
  33.     else { 
  34.     controlForm = parent.frames[1].document.controlForm
  35.     controlForm.firstname.value=form1.firstname.value
  36.     controlForm.lastname.value=form1.lastname.value
  37.     controlForm.Address.value=form1.Address.value
  38.     controlForm.city.value=form1.city.value
  39.     controlForm.postcode.value=form1.postcode.value
  40.     controlForm.dateofbirth.value=form1.dateofbirth.value
  41.     controlForm.contactphonenumber.value=form1.contactphonenumber.value
  42.     controlForm.emailaddress.value=form1.emailaddress.value
  43.     controlForm.mypassword.value=form1.mypassword.value 
  44.     }
  45.  
  46.  
  47.  function PhoneNumber{
  48.  
  49.      if (form1.checkphonenumber.value==""){
  50.     alert("you must fill in your contactphonenumber")
  51.     formOne.checkphonenumber.focus();
  52.     }
  53.  
  54.     var stripped = checkphonenumber.replace(/[\(\)\.\-\ ]/g, '');
  55.     //strip out acceptable non-numeric characters
  56.  
  57.     else if (isNaN(parseInt(stripped))) {
  58.        alert("the phone number contains illegal characters")}
  59.  
  60.  
  61.     else if (!(stripped.length == 11)) {
  62.     alert("The phone number is the wrong length. Make sure you included an area code.")}
  63.  
  64.     }
  65.  
  66.  
  67. function checkEmail{
  68.  
  69.     var emailFilter=/^.+@.+\..{2,3,4,6}$/;
  70.  
  71.      if (form1.emailaddress.value==""){
  72.     alert("you must fill in your email address")
  73.     formOne.emailaddress.focus();
  74.     }
  75.  
  76.     else if (!(emailFilter.test(emailaddress))) { 
  77.           alert("Enter a valid email add")
  78.     }
  79.  
  80.     else if (form1.mypassword.value==""){
  81.     alert("you must fill in your password")
  82.     formOne.mypassword.focus();}
  83.  
  84.     }
  85.  
  86. function checkPassword{
  87.  
  88.  
  89.     var illegalChars = /\W/;
  90.  
  91.      if ((mypassword.length < 6) || (mypassword.length > 8)) {
  92.            alert("Password should have atleast 6 characters but no more than 7.")
  93.     formOne.mypassword.focus();
  94.     }
  95.  
  96.        else if (illegalChars.test(mypassword)) {
  97.            alert("The password can only contain letters and numbers")} 
  98.  
  99.  
  100.  
  101.     }
  102.  
  103. </script>
  104. </head>
  105.  
  106.  
  107. <body>
  108.  
  109. <form method="GET" action="http://localhost:8081/MyServlet" NAME="formOne" >
  110.  
  111.  
  112. <center>
  113.  
  114. <h1>Registration Details</h1>
  115.  
  116. <br><br>
  117.  
  118. First Name <input type="text" name="firstname" value=""><br><br>
  119.  
  120. Last Name <input type="text" name="lastname" value=""><br><br>
  121.  
  122. Address <input type="text" name="address" value=""><br><br>
  123.  
  124. City <input type="text" name="city" value=""><br><br>
  125.  
  126. County (optional) <input type="text" name="county" value=""><br><br>
  127.  
  128. Post Code <input type="text" name="postcode" value=""><br><br>
  129.  
  130. Country <input type="text" name="country" value=""><br><br>
  131.  
  132. Contact Phone Number <input type="text" name="contactphonenumber" value=""><br><br>
  133.  
  134. Date of Birth <input type="text" name="dateofbirth" value=""><br><br>
  135.  
  136. Email Address <input type="text" name="emailaddress" value=""><br><br>
  137.  
  138. Password (minimum 6 characters) <INPUT TYPE=PASSWORD NAME="mypassword"><br><br>
  139.  
  140. <input type="BUTTON" NAME="Submit"  value="--SUBMIT--" onClick="processForm1()">
  141.  
  142. </center>
  143.  
  144. </form>
  145.  
  146. </body>
  147.  
  148. </html>
  149.  
Feb 28 '07 #1
7 1175
iam_clint
1,208 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head><title>Registration Details</title>
  3. <SCRIPT LANGUAGE="JavaScript">
  4. function processForm1() {
  5. var form1 = document.forms.formOne
  6.  if (form1.firstname.value==""){
  7.  alert("you must fill in your first name")
  8.  formOne.firstname.focus();
  9.  }
  10.  else if (form1.lastname.value==""){
  11.  alert("you must fill in your lastname")
  12.  formOne.lastname.focus();
  13.  }
  14.  else if (form1.postcode.value==""){
  15.  alert("you must fill in your postcode")
  16.  formOne.postcode.focus();
  17.  }
  18.  else if (form1.dateofbirth.value==""){
  19.  alert("you must fill in your DOB")
  20.  formOne.dateofbirth.focus();
  21.  }
  22.  
  23.  function PhoneNumber() {
  24.   if (form1.checkphonenumber.value==""){
  25.  alert("you must fill in your contactphonenumber")
  26.  formOne.checkphonenumber.focus();
  27.  }
  28.  var stripped = checkphonenumber.replace(/[\(\)\.\-\ ]/g, '');
  29.  //strip out acceptable non-numeric characters
  30.  if (isNaN(parseInt(stripped))) {
  31.     alert("the phone number contains illegal characters")}
  32.  
  33.  else if (!(stripped.length == 11)) {
  34.  alert("The phone number is the wrong length. Make sure you included an area code.")}
  35.  
  36.  }
  37.  
  38. function checkEmail() {
  39.  var emailFilter=/^.+@.+\..{2,3,4,6}$/;
  40.   if (form1.emailaddress.value==""){
  41.  alert("you must fill in your email address")
  42.  formOne.emailaddress.focus();
  43.  }
  44.  else if (!(emailFilter.test(emailaddress))) { 
  45.        alert("Enter a valid email add")
  46.  }
  47.  else if (form1.mypassword.value==""){
  48.  alert("you must fill in your password")
  49.  formOne.mypassword.focus();}
  50.  
  51.  }
  52. function checkPassword() {
  53.  
  54.  var illegalChars = /\W/;
  55.   if ((mypassword.length < 6) || (mypassword.length > 8)) {
  56.         alert("Password should have atleast 6 characters but no more than 7.")
  57.  formOne.mypassword.focus();
  58.  }
  59.     else if (illegalChars.test(mypassword)) {
  60.         alert("The password can only contain letters and numbers")} 
  61.  
  62.  
  63.  
  64.  }
  65. </script>
  66. </head>
  67.  
  68. <body>
  69. <form method="GET" action="http://localhost:8081/MyServlet" NAME="formOne" >
  70.  
  71. <center>
  72. <h1>Registration Details</h1>
  73. <br><br>
  74. First Name <input type="text" name="firstname" value=""><br><br>
  75. Last Name <input type="text" name="lastname" value=""><br><br>
  76. Address <input type="text" name="address" value=""><br><br>
  77. City <input type="text" name="city" value=""><br><br>
  78. County (optional) <input type="text" name="county" value=""><br><br>
  79. Post Code <input type="text" name="postcode" value=""><br><br>
  80. Country <input type="text" name="country" value=""><br><br>
  81. Contact Phone Number <input type="text" name="contactphonenumber" value=""><br><br>
  82. Date of Birth <input type="text" name="dateofbirth" value=""><br><br>
  83. Email Address <input type="text" name="emailaddress" value=""><br><br>
  84. Password (minimum 6 characters) <INPUT TYPE=PASSWORD NAME="mypassword"><br><br>
  85. <input type="BUTTON" NAME="Submit"  value="--SUBMIT--" onClick="processForm1()">
  86. </center>
  87. </form>
  88. </body>
  89. </html>
  90.  
give this a try
Feb 28 '07 #2
Naha
41
With the code you gave only the first function works the others do not.



Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head><title>Registration Details</title>
  3. <SCRIPT LANGUAGE="JavaScript">
  4. function processForm1() {
  5. var form1 = document.forms.formOne
  6.  if (form1.firstname.value==""){
  7.  alert("you must fill in your first name")
  8.  formOne.firstname.focus();
  9.  }
  10.  else if (form1.lastname.value==""){
  11.  alert("you must fill in your lastname")
  12.  formOne.lastname.focus();
  13.  }
  14.  else if (form1.postcode.value==""){
  15.  alert("you must fill in your postcode")
  16.  formOne.postcode.focus();
  17.  }
  18.  else if (form1.dateofbirth.value==""){
  19.  alert("you must fill in your DOB")
  20.  formOne.dateofbirth.focus();
  21.  }
  22.  
  23.  function PhoneNumber() {
  24.   if (form1.checkphonenumber.value==""){
  25.  alert("you must fill in your contactphonenumber")
  26.  formOne.checkphonenumber.focus();
  27.  }
  28.  var stripped = checkphonenumber.replace(/[\(\)\.\-\ ]/g, '');
  29.  //strip out acceptable non-numeric characters
  30.  if (isNaN(parseInt(stripped))) {
  31.     alert("the phone number contains illegal characters")}
  32.  
  33.  else if (!(stripped.length == 11)) {
  34.  alert("The phone number is the wrong length. Make sure you included an area code.")}
  35.  
  36.  }
  37.  
  38. function checkEmail() {
  39.  var emailFilter=/^.+@.+\..{2,3,4,6}$/;
  40.   if (form1.emailaddress.value==""){
  41.  alert("you must fill in your email address")
  42.  formOne.emailaddress.focus();
  43.  }
  44.  else if (!(emailFilter.test(emailaddress))) { 
  45.        alert("Enter a valid email add")
  46.  }
  47.  else if (form1.mypassword.value==""){
  48.  alert("you must fill in your password")
  49.  formOne.mypassword.focus();}
  50.  
  51.  }
  52. function checkPassword() {
  53.  
  54.  var illegalChars = /\W/;
  55.   if ((mypassword.length < 6) || (mypassword.length > 8)) {
  56.         alert("Password should have atleast 6 characters but no more than 7.")
  57.  formOne.mypassword.focus();
  58.  }
  59.     else if (illegalChars.test(mypassword)) {
  60.         alert("The password can only contain letters and numbers")} 
  61.  
  62.  
  63.  
  64.  }
  65. </script>
  66. </head>
  67.  
  68. <body>
  69. <form method="GET" action="http://localhost:8081/MyServlet" NAME="formOne" >
  70.  
  71. <center>
  72. <h1>Registration Details</h1>
  73. <br><br>
  74. First Name <input type="text" name="firstname" value=""><br><br>
  75. Last Name <input type="text" name="lastname" value=""><br><br>
  76. Address <input type="text" name="address" value=""><br><br>
  77. City <input type="text" name="city" value=""><br><br>
  78. County (optional) <input type="text" name="county" value=""><br><br>
  79. Post Code <input type="text" name="postcode" value=""><br><br>
  80. Country <input type="text" name="country" value=""><br><br>
  81. Contact Phone Number <input type="text" name="contactphonenumber" value=""><br><br>
  82. Date of Birth <input type="text" name="dateofbirth" value=""><br><br>
  83. Email Address <input type="text" name="emailaddress" value=""><br><br>
  84. Password (minimum 6 characters) <INPUT TYPE=PASSWORD NAME="mypassword"><br><br>
  85. <input type="BUTTON" NAME="Submit"  value="--SUBMIT--" onClick="processForm1()">
  86. </center>
  87. </form>
  88. </body>
  89. </html>
  90.  
give this a try[/quote]
Feb 28 '07 #3
acoder
16,027 Expert Mod 8TB
With the code you gave only the first function works the others do not.
Because you haven't defined form1 to be the form. Add the following (as in the first function) to the beginning of the remaining functions:
Expand|Select|Wrap|Line Numbers
  1. var form1 = document.forms.formOne;
Feb 28 '07 #4
Naha
41
no, the remaining functions still are not working.
Mar 1 '07 #5
acoder
16,027 Expert Mod 8TB
You need to use one or the other: form1 or formOne (whichever one you have defined). Don't use them interchangeably.

Use
Expand|Select|Wrap|Line Numbers
  1. var form1 = document.formOne;
instead of what you currently have. Currently your focus code for each if branch is incorrect - it is using formOne, but you haven't defined it.

Similarly, you are accessing fields improperly. You have to access them through the form object, e.g.
Expand|Select|Wrap|Line Numbers
  1. var mypassword = form1.mypassword;
where form1 is the form object. Now you can use mypassword.
Mar 1 '07 #6
Naha
41
I made the changes but its still no working.





Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head><title>Registration Details</title>
  3. <SCRIPT LANGUAGE="JavaScript">
  4.  
  5. function processForm1() {
  6. var form1 = document.formOne;
  7.  if (form1.firstname.value==""){
  8.  alert("you must fill in your first name")
  9.  formOne.firstname.focus();
  10.  }
  11.  else if (form1.lastname.value==""){
  12.  alert("you must fill in your lastname")
  13.  formOne.lastname.focus();
  14.  }
  15.  else if (form1.postcode.value==""){
  16.  alert("you must fill in your postcode")
  17.  formOne.postcode.focus();
  18.  }
  19.  else if (form1.dateofbirth.value==""){
  20.  alert("you must fill in your DOB")
  21.  formOne.dateofbirth.focus();
  22.  }
  23.  
  24. function checkPhoneNumber() {
  25. var form1 = document.formOne;
  26. var contactphonenumber = form1.contactphonenumber;
  27.   if (form1.contactphonenumber.value==""){
  28.  alert("you must fill in your contactphonenumber")
  29.  formOne.contactphonenumber.focus();
  30.  }
  31.  var stripped = contactphonenumber.replace(/[\(\)\.\-\ ]/g, '');
  32.  //strip out acceptable non-numeric characters
  33.  if (isNaN(parseInt(stripped))) {
  34.     alert("the phone number contains illegal characters")}
  35.  
  36.  else if (!(stripped.length == 11)) {
  37.  alert("The phone number is the wrong length. Make sure you included an area code.")}
  38.  
  39. }
  40.  
  41. function checkEmail() {
  42. var form1 = document.formOne;
  43. var emailaddress = form1.emailaddress;
  44. var emailFilter=/^/w+([/.-]?/w+)*@;
  45.   if (form1.emailaddress.value==""){
  46.  alert("you must fill in your email address")
  47.  formOne.emailaddress.focus();
  48.  }
  49.  else if (!(emailFilter.test(emailaddress)))
  50. {
  51.        alert("Enter a valid email add")
  52.          formOne.emailaddress.focus();
  53.  }
  54.  
  55.  }
  56.  
  57. function checkPassword() {
  58. var form1 = document.formOne;
  59. var mypassword = form1.mypassword;
  60.  var illegalChars = /\W/;
  61.   if ((mypassword.length < 6) || (mypassword.length > 8)) {
  62.         alert("Password should have atleast 6 characters but no more than 7.")
  63.  formOne.mypassword.focus();
  64.  }
  65.     else if (illegalChars.test(mypassword)) {
  66.         alert("The password can only contain letters and numbers")} 
  67.  
  68.  }
  69.  
  70.  
  71. </script>
  72. </head>
  73.  
  74. <body>
  75. <form method="GET" action="http://localhost:8081/MyServlet" NAME="formOne"    >
  76.  
  77. <center>
  78. <h1>Registration Details</h1>
  79. <br><br>
  80. First Name <input type="text" name="firstname" value=""><br><br>
  81. Last Name <input type="text" name="lastname" value=""><br><br>
  82. Address <input type="text" name="address" value=""><br><br>
  83. City <input type="text" name="city" value=""><br><br>
  84. County (optional) <input type="text" name="county" value=""><br><br>
  85. Post Code <input type="text" name="postcode" value=""><br><br>
  86. Country <input type="text" name="country" value=""><br><br>
  87. Contact Phone Number <input type="text" name="contactphonenumber" value=""><br><br>
  88. Date of Birth <input type="text" name="dateofbirth" value=""><br><br>
  89. Email Address <input type="text" name="emailaddress" value=""><br><br>
  90. Password (minimum 6 characters) <INPUT TYPE=PASSWORD NAME="mypassword"><br><br>
  91. <input type="BUTTON" NAME="Submit"  value="--SUBMIT--" onClick="processForm1() && checkEmail() && checkPassword() && checkPhoneNumber()">
  92. </center>
  93. </form>
  94. </body>
  95. </html> 
  96.  
  97.  
Mar 1 '07 #7
acoder
16,027 Expert Mod 8TB
You've still not replaced formOne with form1, e.g. see below:
Expand|Select|Wrap|Line Numbers
  1. function processForm1() {
  2. var form1 = document.formOne;
  3.  if (form1.firstname.value==""){
  4.  alert("you must fill in your first name")
  5.  form1.firstname.focus();
  6.  }
  7.  else if (form1.lastname.value==""){
  8.  alert("you must fill in your lastname")
  9.  form1.lastname.focus();
  10.  }
  11.  else if (form1.postcode.value==""){
  12.  alert("you must fill in your postcode")
  13.  form1.postcode.focus();
  14.  }
  15.  else if (form1.dateofbirth.value==""){
  16.  alert("you must fill in your DOB")
  17.  form1.dateofbirth.focus();
  18.  }
  19. }
Also, you didn't close the first function.

Your email regular expression is incorrect - the forward slashes should be back slashes (\w instead of /w). Also see this page for a better reg.exp. for emails.
Mar 1 '07 #8

Post your reply

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

Similar topics

reply views Thread by babash | last post: by
reply views Thread by Ashutosh | last post: by
1 post views Thread by Murat YILMAZ | last post: by
2 posts views Thread by anil kumar via .NET 247 | last post: by
84 posts views Thread by Patient Guy | last post: by
2 posts views Thread by swethak | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.