473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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 1917
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

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

Similar topics

15
by: Michael Hill | last post by:
I have this cf script below where the javascript in it works perfectly i.e. a new window opens when I press 'click me' when I call it up using the url in the browser. But when I "POST" to it...
6
by: Penny | last post by:
Hi all, I've built a simple search <Form> on a web page that is intended to allow the user to search a record store database. There is a drop down box where the user can choose either 'Artist'...
2
by: ugurceng | last post by:
Hi, I have an aspx page that includes both search and insert screen , When I click the the insert button ,if required fields are blank then the validation.js throws an error, but when I click...
4
by: devanoy | last post by:
I have a piece of c# code in the Page_Load function. submitButton.Attributes.Add("onClick", "return checkForm('" + this + "','" + SomeTextBox.Text + "')"); When I click the submitButton, it...
5
by: rocknbil | last post by:
Hello everyone! I'm new here but have been programming for the web in various languages for 15 years or so. I'm certainly no "expert" but can keep myself out of trouble (or in it?) most of the time....
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
12
by: SA SA | last post by:
I know it is me but i can not get this condition to work. Regardless of Check or credit card it always defaults to else portion of the script. I can't get if (login.PMT_INDICATOR.value == "C") to...
1
by: nitinp | last post by:
hello, I am trying to validate empty textbox value by javascript in Firefox. It is working fine in IE but not in Firefox. Note - code is for ASP.NET <script runat="server"> protected void...
4
by: jshokoo | last post by:
can someone help me with the code below .there is no call to the checkForm function.I have just included the part to validate the name text area platform: rational v6 os : xppro...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.