473,407 Members | 2,326 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,407 software developers and data experts.

Input validation help

38
I am trying to validate the inputs of a form using javascript.
The validation is working but the form is not been submitted after the inputs have been checked.
I have been unable to find any error.
Any help will be appreciated.
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5.   <title>FORM</title>
  6.   <script LANGUAGE="JavaScript">
  7. <!--
  8. function ValidateForm(form){
  9. ErrorText= "";
  10. if ( ( form.usage[0].checked == false ) && ( form.usage[1].checked == false )&&( form.usage[2].checked == false ) ) { alert ( "Please answer the question:Where will you be using your laptop? " ); return false; }
  11. if ( ( form.outside[0].checked == false ) && ( form.outside[1].checked == false ) ) { alert ( "Please answer the question:will you be using you laptop on the trains or outside at all? " ); return false; }
  12. if ( ( form.games[0].checked == false ) && ( form.games[1].checked == false ) ) { alert ( "Please answer the question:Do you play video games? " ); return false; }
  13. if ( ( form.storage[0].checked == false ) && ( form.storage[1].checked == false ) ) { alert ( "Please answer the question:Do you need lot of storage for your files? " ); return false; }
  14. if ( ( form.type[0].checked == false ) && ( form.type[1].checked == false ) ) { alert ( "Please answer the question:Do you use  MSN messenger,Skype or play DVD's? " ); return false; }
  15. if ( ( form.concern[0].checked == false ) && ( form.concern[1].checked == false )&& ( form.concern[2].checked == false ) ) { alert ( "Please answer the question:What is your main concern? " ); return false; }
  16. if (ErrorText= "") { form.submit() }
  17. }
  18. -->
  19. </script>
  20. </head>
  21. <body  bgcolor="Wheat">
  22.  
  23. <form name="form" action="handle_form_final.php"   method="post">
  24. <fieldset><legend> Select your Laptop preferences here:</legend>
  25. <p><b> Where will you be using your laptop?</b> <input type="radio" name="usage" value="home"/>Home
  26.             <input type="radio" name="usage" value="office"/>Office<input type="radio" name="usage" value="both"/>Both</p>
  27. <p><b> will you be using you laptop on the trains or outside at all?</b> <input type="radio" name="outside" value="yes"/>Yes
  28.             <input type="radio" name="outside" value="no"/>No</p>
  29. <p><b>Do you play video games?</b> <input type="radio" name="games" value="yes"/>Yes
  30.             <input type="radio" name="games" value="no"/>No</p>
  31. <p><b> what is your budget range?</b>
  32.             <select name=" Budget_Range" >
  33.             <option value="low">Under £500</option>
  34.             <option value="mid">Under £800</option>
  35.             <option value="high">Over £800</option>
  36.             </select>
  37. <p><b>Do you need lot of storage for your files?</b> <input type="radio" name="storage" value="yes"/>Yes
  38.             <input type="radio" name="storage" value="no"/>No</p>
  39.  
  40. <p><b>Do you use  MSN messenger,Skype or play DVD's?</b> <input type="radio" name="type" value="yes"/>Yes
  41.             <input type="radio" name="type" value="no"/>No</p>
  42. <p><b> What is your main concern?</b> <input type="radio" name="concern" value="price"/>Price
  43.             <input type="radio" name="concern" value="mobility"/>Mobility<input type="radio" name="concern" value="power"/>Power</p>
  44.  </fieldset>
  45.  
  46.  <div align="center"><input type="button" name="submit" value="Start Search" onClick="ValidateForm(this.form)"></div>
  47.  <input type="reset" value="Reset">
  48. </form>
  49.  
  50.  
  51.  
  52. </body>
  53. </html>
  54.  

Thanks
Feb 3 '09 #1
2 1528
Hi, poreko!

You have two mistakes in your code:

This:

Expand|Select|Wrap|Line Numbers
  1. if (ErrorText = "") { form.submit() }
  2.  
Should become:

Expand|Select|Wrap|Line Numbers
  1. if (ErrorText == "") { form.submit() }
  2.  

Second, you have a button named submit and even using a local variable form in your function it conflicts with the global form name which also is form. So, when you're accessing:

Expand|Select|Wrap|Line Numbers
  1. form.submit()
  2.  
the browser thinks you're trying to get the button.

Rename your button to btSubmit.

Additional notes:

1. Why use the variable ErrorText at all? you can simply remove the variable and the test you're making because if one of your validations fails you're returning false.

2. I like to use the onsubmit event of the form when validating input because you can simply return true or false and you don't have to manually call the form's submit method:

The function would look like this:

Expand|Select|Wrap|Line Numbers
  1. function ValidateForm(form){
  2.  
  3.     if ( ( form.usage[0].checked == false ) && ( form.usage[1].checked == false )&&( form.usage[2].checked == false ) ) { alert ( "Please answer the question:Where will you be using your laptop? " ); return false; }
  4.     if ( ( form.outside[0].checked == false ) && ( form.outside[1].checked == false ) ) { alert ( "Please answer the question:will you be using you laptop on the trains or outside at all? " ); return false; }
  5.     if ( ( form.games[0].checked == false ) && ( form.games[1].checked == false ) ) { alert ( "Please answer the question:Do you play video games? " ); return false; }
  6.     if ( ( form.storage[0].checked == false ) && ( form.storage[1].checked == false ) ) { alert ( "Please answer the question:Do you need lot of storage for your files? " ); return false; }
  7.     if ( ( form.type[0].checked == false ) && ( form.type[1].checked == false ) ) { alert ( "Please answer the question:Do you use  MSN messenger,Skype or play DVD's? " ); return false; }
  8.     if ( ( form.concern[0].checked == false ) && ( form.concern[1].checked == false )&& ( form.concern[2].checked == false ) ) { alert ( "Please answer the question:What is your main concern? " ); return false; }
  9.  
  10.     return true;
  11. }
  12.  
on your form declaration you put:

Expand|Select|Wrap|Line Numbers
  1. <form name="form" action="handle_form_final.php" method="post" onsubmit="return ValidateForm(this);">
  2.  
and your submit button would look like this:

Expand|Select|Wrap|Line Numbers
  1. <input type="submit" name="btSubmit" value="Start Search">
  2.  
Hope this helps.

Best regards!
Feb 4 '09 #2
poreko
38
Thank a lot ivosilva it is working now
Feb 4 '09 #3

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

Similar topics

12
by: Robert | last post by:
As a pre-newbie to visual basic, I haven't programmed for years and I am having the greatest of difficulties coping with it. If any kind soul can help me out with advice then I'd be most grateful....
7
by: F. Michael Miller | last post by:
I have a db with Access front end, sql back, linked tables. I need to be able to change input masks at the table level in code. Any ideas? Thanks!
4
by: John Slate | last post by:
I have built a simple web form that uses input validation. I use the EnableClientScript option to produce a javascript alert box when input errors occur. The only validation is a password...
1
by: Paul | last post by:
Hi, I'm developing a search facility for my web app. In order for the search criteria to be valid the user must (a) enter some text in a text box and check and least one checkbox (of which there...
2
by: headware | last post by:
I realize that when making a web application, performing input validation in the browser is good because it prevents postbacks. However, input checking that goes beyond making sure a value exists...
1
by: John Slate | last post by:
I have built a simple form that uses input validation. I use the EnableClientScript option to produce a javascript alert box when input errors occur. The only validation is a password confirmation...
10
by: canam | last post by:
Hi: I hope someone can help me. Is there a way to add a password to a particular field in the event that the validation rule must be over written? I've created an input form in a...
1
by: SkipNRun | last post by:
I am a novice when comes to JavaScript, AJAX. I am working on a form, which will allow users to update their contact information. In order to make the form flexible, I need to use pull down list. ...
18
vikas251074
by: vikas251074 | last post by:
I am using ASP In my company, all employees have to sent materials out of premises. Any employee who need to sent material out will use this web application. In the first page, an employee enters...
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: 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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.