469,903 Members | 2,218 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Checks and Balances in HTML form


I am creating a form in HTML. When the user fills out the form and hits the submit button, the program verifies if all the fields in the form have been filled out or not. If the user accidentally missed a field, then a prompt would appear and display what field needs to be entered.

How do I go about doing this?

Dec 11 '06 #1
1 1049
My tip would be for you to use javascript to solve the problem.

Here's a layout... there's a lot of information on google though :p
Expand|Select|Wrap|Line Numbers
  1. function checkForm(form)
  2. {
  3.     var string = "";
  4.     string = string + validate_someField();
  5.     if (string.length != 0) {
  6.         alert(string);
  7.         return false;
  8.     }
  9.     return true;
  10. }
  12. function validate_someField()
  13. {
  14.     var f = document.getElementById("fieldID");   //whatever the ID is for the input-object
  15.     if( f.value.length < 1 )    // if empty - write alert
  16.         return "You haven't filled in the field XX.";
  17.     return "";
  18. }
And then you connect the function to the form by adding

Expand|Select|Wrap|Line Numbers
  1. onsubmit="return checkForm(this);"
in the <form>-tag.

And the javascript code can either added as a .js-file in <head></head>
Expand|Select|Wrap|Line Numbers
  1. <script src="validationFile.js" type="text/javascript"></script>
or directly as some code in the <head></head>

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.        the code
  3. </script>
Dec 12 '06 #2

Post your reply

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

Similar topics

2 posts views Thread by MrMagooba | last post: by
reply views Thread by Steven Stewart | last post: by
6 posts views Thread by Nicola Mezzetti | last post: by
5 posts views Thread by Ganesh | last post: by
reply views Thread by John Devlon | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.