473,407 Members | 2,320 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.

validation form project

im fairly new at javascript but i need to write a script for a project and don't know where to begin. basically it involves form validation... i guess here's the breakdown.

I have created a form in html already and need to figure out how to write this script.

1.
select any number of products (checkboxes) and set quantities for each. Must set at least one product and selected products must have a quantity value > 0.

2. The second portion involves shipping information (ie. Name, Address, City, State, Zip, phone number, and email).
The fields must all be filled in. The state must be a valid state code (two letters). The phone number must be all digits and 10 digits. The email must be formed at least like x@y.

3. i then need to have a subtotal of the items, based on how many were selected and such.

any help would be greatly appreciated,
thanks in advance.
Jan 30 '07 #1
9 1608
acoder
16,027 Expert Mod 8TB
Check out the following link to get you started. If you get stuck post again.

An earlier thread with links to form validation tutorials
Jan 30 '07 #2
yea i know i posted this earlier.. ive read up on it and still quite unfamiliar with the language.. i guess ill show u a snippet of the code when i figure out how to write it.
Jan 30 '07 #3
acoder
16,027 Expert Mod 8TB
If you're not familiar with Javascript, then perhaps you should go through a tutorial.
Jan 31 '07 #4
Okay here is what I have now. I want to make the checkboxes indicate the value ="1" when clicked and value =" " (empty) when unclicked. So far I have managed to get the first step done but unsure as how to set it to an empty value when it is unclicked.


[HTML]<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Validation Form</title>

<script>

function checkValid()

{
if

(document.myForm.ckbox1.checked == false &&
document.myForm.ckbox2.checked == false &&
document.myForm.ckbox3.checked == false)

{
alert ("You didn't select any product");
return false;
}

else
{
return true;
}
}

</script>
</head>

<body bgcolor="#555555">

<form action="#" method="post" name="myForm" id="myForm" onsubmit="return checkValid();">
<table align="center" border="1">
<tr>
<td><input type="checkbox" id="ckbox1" name="ckbox1" onclick="document.myForm.p1.value=1;" /> Product 1</td>
<td>@ 1.25 <input type="text" id="p1" name="p1" size="5" /> Quantity</td>
</tr>
<tr>
<td><input type="checkbox" id="ckbox2" name="ckbox2" /> Product 2</td>
<td>@ 2.25 <input type="text" id="p2" name="p2" size="5" /> Quantity</td>
</tr>
<tr>
<td><input type="checkbox" id="ckbox3" name="ckbox3" /> Product3</td>
<td>@ 3.25 <input type="text" id="p3" name="p3" size="5" /> Quantity</td>

</tr>
<tr>
<td>Ship to:</td>
<td>
<table border="1">
<tr>
<td>Name:</td>
<td><input type="text" name="name" id="name" /></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="address" id="address" /></td>
</tr>
<tr>
<td>City:</td>
<td><input type="text" name="city" id="city" /></td>
</tr>
<tr>
<td>State:</td>
<td>
<select>
<option value="">--Select State--</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="PR">Puerto Rico</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
</td>
</tr>
<tr>
<td>Zip:</td>
<td><input type="text" name="zip" id="zip" /></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="text" name="phone" id="phone" /></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" id="email" /></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>Shipping:</td>
<td>
<select>
<option value="standard">Standard</option>
<option value="2-3 days">2-3 days</option>
<option value="Overnight">Overnight</option>
</select>
</td>
</tr>
<tr>
<td><input type="button" value="subtotal" name="subtotal" id="subtotal" /></td>
<td><input type="text" name="total" id="total" /></td>
</tr>
<tr>
<td><input type="radio" name="mc" id="mc" />MasterCard</td>
<td rowspan="2">
<input type="text" name="total" id="total" />
</td>
</tr>
<tr>
<td><input type="radio" name="visa" id="visa" />Visa</td>
</tr>
<tr>
<td><input type="submit" name="submit" id="submit" value="submit" /></td>
<td><input type="reset" name="reset" id="reset" value="reset" /></td>
</table>
</form>

</body>
</html>[/HTML]
Feb 2 '07 #5
acoder
16,027 Expert Mod 8TB
Add the following function:
Expand|Select|Wrap|Line Numbers
  1. function setVal(chkd, num) {
  2. var val = "";
  3. if (chkd) val = 1;
  4. document.myForm["p"+num].value = val;
  5. }
It sets the val to empty assuming that it's not checked. Then checks for chkd value. If checked, changes val to 1. Finally, set the p1 (or p2/p3/whatever) to the value. If you need more explanation, post again.

To call the function, in your onclick call:
Expand|Select|Wrap|Line Numbers
  1. onclick="setVal(this.checked,1);"
this.checked takes the checked property of the checkbox. Also, pass 1,2 or 3 depending on the number of the checkbox to determine which text box to set.

Hope that helps.
Feb 2 '07 #6
senaka
23
jasonchan,

you mean you need a required field validator is it? that means if @least one of what you require is not filled your form must not get submitted. Is that what you want?

Rgds.
Feb 2 '07 #7
jasonchan,

you mean you need a required field validator is it? that means if @least one of what you require is not filled your form must not get submitted. Is that what you want?

Rgds.
yea thats what i wanted... i think acoder helped a lot with that code. thanks again..

i also want to embed a validation function for the main verify function but it doesnt seem to work at all.

This is what i got:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  6. <title>Validation Form</title>
  7.  
  8. <script>
  9.  
  10.  
  11. var s = false;
  12.  
  13. function verify(form) {
  14.  
  15. /* At least one product checkbox needs to be checked */
  16.  
  17.   if     
  18.  
  19.     (document.myForm.ckbox1.checked == false &&
  20.     document.myForm.ckbox2.checked == false &&
  21.     document.myForm.ckbox3.checked == false)
  22.  
  23.     {
  24.         alert ("You didn't select any product");
  25.         return false;
  26.     }
  27.  
  28.  
  29.  
  30.  
  31.  
  32. function validateZIP( document.myForm.zip.value) {
  33.  
  34. var valid = "0123456789-";
  35. var hyphencount = 0;
  36.  
  37. if (document.myForm.zip.value.length!=5 && document.myForm.zip.value.length!=10) {
  38. alert("Please enter your 5 digit or 5 digit+4 zip code.");
  39. return false;
  40. }
  41. for (var i=0; i < document.myForm.zip.value.length; i++) {
  42. temp = "" + field.substring(i, i+1);
  43. if (temp == "-") hyphencount++;
  44. if (valid.indexOf(temp) == "-1") {
  45. alert("Invalid characters in your zip code.  Please try again.");
  46. return false;
  47. }
  48. if ((hyphencount > 1) || ((document.myForm.zip.value.length==10) && ""+document.myForm.zip.value.charAt(5)!="-")) {
  49. alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
  50. return false;
  51.    }
  52. }
  53. return true;
  54. }
  55.  
  56.  
  57.  
  58.  
  59. /* VALID PHONE NUMBER */
  60.  
  61.     // Declaring required variables
  62.     var digits = "0123456789";
  63.     // non-digit characters which are allowed in phone numbers
  64.     var phoneNumberDelimiters = "()- ";
  65.     // characters which are allowed in international phone numbers
  66.     // (a leading + is OK)
  67.     var validWorldPhoneChars = phoneNumberDelimiters + "+";
  68.     // Minimum no of digits in an international phone no.
  69.     var minDigitsInIPhoneNumber = 10;
  70.  
  71.     function isInteger(s)
  72.     {   var i;
  73.         for (i = 0; i < s.length; i++)
  74.         {   
  75.  
  76.         // Check that current character is number.
  77.             var c = s.charAt(i);
  78.             if (((c < "0") || (c > "9"))) return false;
  79.         }
  80.         // All characters are numbers.
  81.         return true;
  82.     }
  83.  
  84.     function stripCharsInBag(s, bag)
  85.     {   var i;
  86.         var returnString = "";
  87.         // Search through string's characters one by one.
  88.         // If character is not in bag, append to returnString.
  89.         for (i = 0; i < s.length; i++)
  90.         {   
  91.             // Check that current character isn't whitespace.
  92.             var c = s.charAt(i);
  93.             if (bag.indexOf(c) == -1) returnString += c;
  94.         }
  95.         return returnString;
  96.     }
  97.  
  98.     function checkInternationalPhone(strPhone){
  99.     s=stripCharsInBag(strPhone,validWorldPhoneChars);
  100.     return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
  101.     }
  102.  
  103.  
  104.  
  105.     var Phone=document.myForm.phonenumber
  106.  
  107.     if ((Phone.value==null)||(Phone.value=="")){
  108.         alert("Please Enter your Phone Number")
  109.         Phone.focus()
  110.         return false
  111.     }
  112.     if (checkInternationalPhone(Phone.value)==false){
  113.         alert("Please Enter a Valid Phone Number")
  114.         Phone.value=""
  115.         Phone.focus()
  116.         return false
  117.     }
  118.  
  119. /* END OF VALID PHONE */
  120.  
  121.  
  122.  
  123.   if(document.myForm.email.value == "") 
  124.  
  125.     {
  126.     alert("Please enter an email");
  127.     return false;
  128.   }
  129.  
  130.  
  131.  
  132. /* VALID EMAIL? */
  133.  
  134. function IsEmailValid(FormName)
  135.  
  136. {
  137.         var EmailOk  = true
  138.         var Temp     = FormName;
  139.         var AtSym    = Temp.value.indexOf('@')
  140.         var Period   = Temp.value.lastIndexOf('.')
  141.         var Space    = Temp.value.indexOf(' ')
  142.         var Length   = Temp.value.length - 1   // Array is from 0 to length-1
  143.  
  144.         if ((AtSym < 1) ||                     // '@' cannot be in first position
  145.     (Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
  146.     (Period == Length ) ||             // Must be atleast one valid char after '.'
  147.     (Space  != -1))                    // No empty spaces permitted
  148.   {
  149.       EmailOk = false
  150.       Temp.focus()
  151.   }
  152.         return EmailOk
  153. }
  154.  
  155.  
  156.  
  157.   if (IsEmailValid(document.myForm.email) == false) {
  158.     alert("Please enter a valid email address");
  159.     return false;
  160.   }
  161.  
  162. /* END OF VALID EMAIL */
  163.  
  164.  
  165.     if(document.myForm.shipping.value == "") {
  166.     alert("Please select a shipping option");
  167.     return false;
  168.   }
  169.  
  170.  
  171.  
  172.   if(document.myForm.creditcard.value == "") {
  173.     alert("Please enter a zip code");
  174.     return false;
  175.   }
  176.  
  177.  
  178.  
  179.  
  180.   s = true;
  181.   return true;
  182.  
  183. }
  184.  
  185. </script>
  186. </head>
  187.  
  188. <body bgcolor="#555555">
  189.  
  190.  
  191. <form action="thankyou.html" method="post" name="myForm" id="myForm" onsubmit="return verify(this);">
  192. <table align="center" border="1">
  193.   <tr>
  194.     <td><input type="checkbox" id="ckbox1" name="ckbox1" onclick="checkboxClick(this); Calc();" /> Product 1</td>
  195.     <td>@ 1.25 <input type="text" id="p1" name="p1" size="5" value="0" onchange="Calc();" onkeypress="return isNumberInput(this, event);" /> Quantity</td>
  196.  
  197.   </tr>
  198.   <tr>
  199.     <td><input type="checkbox" id="ckbox2" name="ckbox2" onclick="checkboxClick(this); Calc();" /> Product 2</td>
  200.     <td>@ 2.25 <input type="text" id="p2" name="p2" size="5" value="0" onchange="Calc();" onkeypress="return isNumberInput(this, event);" /> Quantity</td>
  201.   </tr>
  202.  <tr>
  203.  
  204.     <td><input type="checkbox" id="ckbox3" name="ckbox3" onclick="checkboxClick(this); Calc();" /> Product3</td>
  205.     <td>@ 3.25 <input type="text" id="p3" name="p3" size="5" value="0" onchange="Calc();" onkeypress="return isNumberInput(this, event);" /> Quantity</td>
  206.  
  207.   </tr>
  208.   <tr>
  209.   <td>Ship to:</td>
  210.   <td>
  211.  
  212.   <table border="1">
  213.     <tr>
  214.       <td>Name:</td>
  215.       <td><input type="text" name="name" id="name" /></td>
  216.     </tr>
  217.     <tr>
  218.       <td>Address:</td>
  219.       <td><input type="text" name="address" id="address" /></td>
  220.  
  221.     </tr>
  222.     <tr>
  223.       <td>City:</td>
  224.       <td><input type="text" name="city" id="city" /></td>
  225.     </tr>
  226.     <tr>
  227.       <td>State:</td>
  228.       <td>
  229.  
  230.       <select name="state" id="state" >
  231.         <option value="">--Select State--</option>
  232.         <option value="AL">AL</option>
  233.         <option value="AK">AK</option>
  234.         <option value="AZ">AZ</option>
  235.         <option value="AR">AR</option>
  236.  
  237.         <option value="CA">CA</option>
  238.         <option value="CO">CO</option>
  239.         <option value="CT">CT</option>
  240.         <option value="DE">DE</option>
  241.         <option value="FL">FL</option>
  242.         <option value="GA">GA</option>
  243.  
  244.         <option value="HI">HI</option>
  245.         <option value="ID">ID</option>
  246.         <option value="IL">IL</option>
  247.         <option value="IN">IN</option>
  248.         <option value="IA">IA</option>
  249.         <option value="KS">KS</option>
  250.  
  251.         <option value="KY">KY</option>
  252.         <option value="LA">LA</option>
  253.         <option value="ME">ME</option>
  254.         <option value="MD">MD</option>
  255.         <option value="MA">MA</option>
  256.         <option value="MI">MI</option>
  257.  
  258.         <option value="MN">MN</option>
  259.         <option value="MS">MS</option>
  260.         <option value="MO">MO</option>
  261.         <option value="MT">MT</option>
  262.         <option value="NE">NE</option>
  263.         <option value="NV">NV</option>
  264.  
  265.         <option value="NH">NH</option>
  266.         <option value="NJ">NJ</option>
  267.         <option value="NM">NM</option>
  268.         <option value="NY">NY</option>
  269.         <option value="NC">NC</option>
  270.         <option value="ND">ND</option>
  271.  
  272.         <option value="OH">OH</option>
  273.         <option value="OK">OK</option>
  274.         <option value="OR">OR</option>
  275.         <option value="PA">PA</option>
  276.         <option value="PR">PR</option>
  277.         <option value="RI">RI</option>
  278.  
  279.         <option value="SC">SC</option>
  280.         <option value="SD">SD</option>
  281.         <option value="TN">TN</option>
  282.         <option value="TX">TX</option>
  283.         <option value="UT">UT</option>
  284.         <option value="VT">VT</option>
  285.  
  286.         <option value="VA">VA</option>
  287.         <option value="WA">WA</option>
  288.         <option value="WV">WV</option>
  289.         <option value="WI">WI</option>
  290.         <option value="WY">WY</option>
  291.       </select>
  292.  
  293.       </td>
  294.     </tr>
  295.     <tr>
  296.       <td>Zip:</td>
  297.       <td><input type="text" name="zip" id="zip" /></td>
  298.     </tr>
  299.     <tr>
  300.       <td>Phone:</td>
  301.  
  302.       <td><input type="text" name="phonenumber" id="phonenumber" /></td>
  303.     </tr>
  304.     <tr>
  305.       <td>Email:</td>
  306.       <td><input type="text" name="email" id="email" /></td>
  307.     </tr>
  308.   </table>
  309.   </td>
  310.  
  311.   </tr>
  312.   <tr>
  313.   <td>Shipping:</td>
  314.   <td>
  315.    Standard @ $3.50: <input type="radio" value="3.50" name="shipping" id="shipping" checked="true" onclick="Calc(this);" />
  316.    2-day @ $5.00: <input type="radio" value="5.00" name="shipping" id="shipping" onclick="Calc(this);" />
  317.    Overnight @ $7.50: <input type="radio" value="7.50" name="shipping" id="shipping" onclick="Calc(this);" />
  318.   </td>
  319.   </tr>
  320.   <tr>
  321.   <td><input type="button" value="subtotal" name="sub" id="sub" onclick="Calc(this);" /></td>
  322.   <td><input type="text" name="subtotal" id="subtotal" readonly="readonly" /></td>
  323.  
  324.   </tr>
  325.   <tr>
  326.       <td><input type="radio" value="mc" name="creditcard" id="creditcard" checked="true" />MasterCard</td>
  327.     <td rowspan="2">
  328.     Please input your credit card number below:<br />
  329.     <input type="text" name="cardnum" id="cardnum" />
  330.     </td>
  331.    </tr>
  332.    <tr>
  333.  
  334.    <td><input type="radio" value="visa" name="creditcard" id="creditcard" />Visa</td>
  335.    </tr>
  336.    <tr>
  337.    <td><input type="submit" name="submit" id="submit" value="submit" /></td>
  338.    <td><input type="reset" name="reset" id="reset" value="reset" /></td>    
  339. </table>
  340. </form>
  341.  
  342. </body>
  343. </html>
  344.  
  345.  
  346.  
  347.  
  348.  
Feb 11 '07 #8
btw i had to take out some parts of the code above bc it was too long to display .. so basically i want to include the validation zip in the original file. any help would be appreciated..thanks again
Feb 11 '07 #9
acoder
16,027 Expert Mod 8TB
You can't declare functions within functions as you have done.

Declare all functions outside then call these within the main verify function.
Feb 12 '07 #10

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

Similar topics

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...
2
by: Shathish | last post by:
Ha i have this problem with the validation control I have a asp.net page which is used to add and edit projects when i'm adding a project the validation controls are working fin but if i select a...
7
by: Ryan Ternier | last post by:
We're running a site that has required field validation on the login page. It works fine on our development / test machines. However, when I upload this site to our live server i get this error. ...
2
by: AnandaSim | last post by:
Hi All, A really puzzling phenomenon. I develop to a Windows 2000 development server in VS 2003. I use Copy Project to deploy the project to a production server. The webpage I'm having...
9
by: JFB | last post by:
Hi Folks, I have a lot of forms and I want to validate de data for text, numbers, phone numbers, email and zipcode How is the best way to perform a validation in all the forms using vb.net? Can I...
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...
5
by: matt | last post by:
hello, i am on an interesting project. in this project, i have to create dynamic data-entry forms for offline-users to fill out, save locally, and eventually postback to our app (when back...
1
by: cwby1966 | last post by:
I am Trying to validate information entered into 4 fields on a form. They can not be empty. If one of the fields is empty then the code should give a message athen stop. If all fields are valid then...
1
by: nRk | last post by:
Hi, I am working on asp.net 2.0 with Visual Studio 2005. My asp.net project contains one Masterpage. I exposed my asp.net project in my machine (Windows XP with IIS 5.1) and access using...
13
by: Andrew Falanga | last post by:
HI, Just a warning, I'm a javascript neophyte. I'm writing a function to validate the contents of a form on a web page I'm developing. Since I'm a neophyte, this function is quite simple at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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.