469,309 Members | 2,793 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Javascript CheckForm

The1corrupted
134 100+
I have a slight dilema on my hands. I want to check this form before submitting it, and making sure everything's filled out. I know there's a way to do it in javascript, but haven't found any methods to do so.

Expand|Select|Wrap|Line Numbers
  1. <script language=\"javascript\" type=\"text/javascript\">
  2.     function checkform(user, password, email) {
  3.         if (user==NULL) {
  4.             alert(\"Check your username!\")
  5.             }
  6.         }
  7.     <script>
  8.  
  9. <form method=\"post\" action=\"".$_SERVER['PHP_SELF']."\">
  10.                     <table style=\"border-style:none;\" align=\"center\">
  11.                         <tr>
  12.                             <td>
  13.                                 Ruler Name:
  14.                             </td>
  15.                             <td>
  16.                                 <input type=\"text\" name=\"user\" length=\"20\">
  17.                             </td>
  18.                         </tr>
  19.                         <tr>
  20.                             <td>
  21.                                 E-mail Address:
  22.                             </td>
  23.                             <td>
  24.                                 <input type=\"text\" name=\"email\" length=\"20\">
  25.                             </td>
  26.                         </tr>
  27.                         <tr>
  28.                             <td>
  29.                                 Password:
  30.                             </td>
  31.                             <td>
  32.                                 <input type=\"password\" name=\"password\" length=\"20\">
  33.                             </td>
  34.                         </tr>
  35.                         <tr>
  36.                             <td>
  37.                                 Empire Name:
  38.                             </td>
  39.                             <td>
  40.                                 <input type=\"text\" name=\"empirename\" length=\"20\">
  41.                             </td>
  42.                         </tr>
  43.                         <tr>
  44.                             <td>
  45.                                 Economic System:
  46.                             </td>
  47.                             <td>
  48.                                 <select name=\"economic\">
  49.                                     <option selected>--------</option>
  50.                                     <option>Capitalist</option>
  51.                                     <option>Facist</option>
  52.                                     <option>Laissez-faire</option>
  53.                                     <option>Natural</option>
  54.                                     <option>Socialist</option>
  55.                                 </select>
  56.                             </td>
  57.  
  58.                             <td>
  59.                                 <input type=\"button\" value=\"Submit\" onClick=\"checkform()\">
  60.                             </td>
  61.                         </tr>
  62.                     </table>
  63.                 </form>
Nov 19 '07 #1
2 1842
The1corrupted
134 100+
Nevermind the onClick function, I'll parse it with an onMouseOver, but I still need help to display an alert message telling you which fields are blank. Preferrably, I would like the errors all to show up in one alert.
Nov 19 '07 #2
gits
5,390 Expert Mod 4TB
hi ...

have a look at the following example that you may use onsubmit of the form you want to check:

Expand|Select|Wrap|Line Numbers
  1. function check_form(f) {
  2.     var errors = [];
  3.     var val    = true;
  4.     var tboxes = f.getElementsByTagName('input');
  5.  
  6.     for (var i = 0; i < tboxes.length; i++) {
  7.         var t = tboxes[i];
  8.  
  9.         if (t.value === '') {
  10.             errors.push(t.name);
  11.         }
  12.     }
  13.  
  14.     if (errors.length > 0) {
  15.         alert('Please fill out the following fields:\n' + errors.join('\n'));
  16.         val = false;
  17.     }
  18.  
  19.     return val;
  20. }
  21.  
call it the following way:

[HTML]<form name="myform" onsubmit="check_form(this);">
[/HTML]
in your html ...

kind regards
Nov 19 '07 #3

Post your reply

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

Similar topics

15 posts views Thread by Michael Hill | last post: by
4 posts views Thread by devanoy | last post: by
27 posts views Thread by Chris | last post: by
12 posts views Thread by SA SA | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.