469,623 Members | 1,520 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

form mandatory fields

Ive been looking through a whole tonne of sites to try to find out how I can make my fields mandatory but havnt been able to find something so far which actually tells me whwta I want to know. Ive tried a couple of code segments but they havnt been working. can anyone tell me how I can make fields in my code mandatory?

My attempt at solving this is below:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.  
  4. <title>Coals Spiral Brochure Enquiry Form</title>
  5.  
  6. <meta name="description" content="">
  7.  
  8. <meta name="keywords" content="">
  9. <script language="javascript" 
  10.   type="text/javascript">
  11.  
  12. function validateForm(brochure1)
  13. {
  14.  
  15. if(""==document.forms.brochure1.firstname.value)
  16. {
  17. alert("Please enter your first name.");
  18. return false;
  19. }
  20.  
  21. if(""==document.forms.brochure1.lastname.value)
  22. {
  23. alert("Please enter your last name.");
  24. return false;
  25. }
  26.  
  27. if(""==document.forms.brochure1.email.value)
  28. {
  29. alert("Please enter your email address.");
  30. return false;
  31. }
  32.  
  33. }
  34. if(""==document.forms.brochure1.phone.value)
  35. {
  36. alert("Please enter your Phone number.");
  37. return false;
  38. }
  39.  
  40. }
  41.  
  42. if(""==document.forms.brochure1.country.value)
  43. {
  44. alert("Please enter your country.");
  45. return false;
  46. }
  47.  
  48. }
  49.  
  50. </script>
  51. </head>
  52. <body bgcolor="white" text="black">
  53.  
  54. <table width="60%" align="center">
  55. <tr>
  56. <td>
  57. <font face=Arial size=2>
  58.  
  59. <IMG SRC="http://www.mdmintec.reachpoint.com/Data/mdmintec/Roche%20MT%20sm.jpg" align="right">
  60. <strong><BR><BR><BR><BR><BR>
  61. Coals Spiral Brochure Enquiry Form</strong>
  62. <HR style="WIDTH: 100%; HEIGHT: 1px" align=center width="100%" color=black>
  63. <BR>
  64. Please fill out your details below in order to receive our Coals Spiral Brochure.
  65. <BR><BR>
  66.  
  67. <form method="post"  onsubmit='return validateForm()' action="http://svr86.ehostpros.com/~minera86/brochure1.php">
  68. <TABLE width=85% align=center cellSpacing=1 cellPadding=1 border=0>
  69. <TR>
  70. <TD>
  71. <font face=Arial size=2>First Name: 
  72. </TD>
  73. <TD>
  74. <INPUT title="Your Google Toolbar can fill this in for you. Select AutoFill" maxLength=100 size=40 name=firstname> 
  75. </TD>
  76. </TR>
  77. <TR>
  78. <TD>
  79. <font face=Arial size=2>Last Name: 
  80. </TD>
  81. <TD>
  82. <INPUT title="Your Google Toolbar can fill this in for you. Select AutoFill" maxLength=100 size=40 name=lastname> 
  83. </TD>
  84. </TR>
  85. <TR>
  86. <TD>
  87. <font face=Arial size=2>Email: 
  88. </TD>
  89. <TD>
  90. <INPUT title="Your Google Toolbar can fill this in for you. Select AutoFill" maxLength=100 size=40 name=email>
  91. </TD>
  92. </TR>
  93. <TR>
  94. <TD>
  95. <font face=Arial size=2>Phone: 
  96. </TD>
  97. <TD>
  98. <INPUT title="Your Google Toolbar can fill this in for you. Select AutoFill" maxLength=100 size=40 name=phone>
  99. </TD>
  100. </TR>
  101. <TR>
  102. <TD>
  103. <font face=Arial size=2>Country: <BR><BR><BR><BR>
  104. </TD>
  105. <TD>
  106. <INPUT title="Your Google Toolbar can fill this in for you. Select AutoFill" maxLength=100 size=40 name=country>
  107. <p Align=right>
  108. <input class="BButton" value="Send" name="cmdSubmit" type="submit">
  109. </align>
  110. </TD>
  111. </TR>
  112. </TABLE>
  113. </form>
  114.  
  115.  
  116. </font>
  117. </body>
  118. </html>
  119.  
  120.  
Oct 29 '07 #1
6 1562
btw this code worked for another site.. but for some reason not this one. Can anyone tell me what Im doing wrong?!
Oct 29 '07 #2
dmjpro
2,476 2GB
btw this code worked for another site.. but for some reason not this one. Can anyone tell me what Im doing wrong?!
What is "brochure1" in "document.forms.brochure1.firstname.value" expression?

Debasis
Oct 29 '07 #3
What is "brochure1" in "document.forms.brochure1.firstname.value" expression?

Debasis
"brochure1" is name of the html file brochure1.html.
Oct 29 '07 #4
dmjpro
2,476 2GB
"brochure1" is name of the html file brochure1.html.
all these expression "document.forms.brochure1" should be changed into "document.forms[0]"

Debasis
Oct 29 '07 #5
all these expression "document.forms.brochure1" should be changed into "document.forms[0]"

Debasis

Why is this? JUst wondering if you could explain it s I understand what I am doing wrong.
Oct 29 '07 #6
acoder
16,027 Expert Mod 8TB
Why is this? JUst wondering if you could explain it s I understand what I am doing wrong.
You can't use "brochure1" because it doesn't refer to the form.

forms[0] is the first form on the page. If you could possibly have more than one form, you would be better off giving your form an id and using document.getElementById(form-id).
Oct 29 '07 #7

Post your reply

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

Similar topics

6 posts views Thread by MAFranklin | last post: by
1 post views Thread by Jason | last post: by
5 posts views Thread by Olly | last post: by
2 posts views Thread by AR123 | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.