der_23
asked on
java script function not working
Below is my script that I used for creating form
but it doesn't work out quite right, I want to add the function that checks
that the phone textfeild are all numbers, the credit card feild are numbers with 16 characters.
I also want to add in the features that when the checkbox is ticked (the value that I set
for the checkbox is the price of each book), the value are added and the total is displayed
below as to how much it add up to.
And also in the name textfield that I have in my form, How can I put in the name of the
user from the cookie as its value.
all suggestions welcome.
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function checkFields() {
missinginfo = "";
if (document.form.name.value == "") {
missinginfo += "\n - Name";
}
if (document.form.address.val ue == "") {
missinginfo += "\n - Address";
}
if (document.form.phone.value == "") {
missinginfo += "\n - Phone";
}
if (document.form.card.value == "") {
missinginfo += "\n - Credit Card Number";
}
if ((document.form.from.value == "") ||
(document.form.from.value. indexOf('@ ') == -1) ||
(document.form.from.value. indexOf('. ') == -1)) {
missinginfo += "\n - Email address";
}
if ((document.form.website.va lue == "") ||
(document.form.website.val ue.indexOf ("http://") == -1) ||
(document.form.website.val ue.indexOf (".") == -1)) {
missinginfo += "\n - Web site";
}
if(document.form.comments. value == "") {
missinginfo += "\n - Comments";
}
if (missinginfo != "") {
missinginfo ="________________________ _____\n" +
"You failed to correctly fill in your:\n" +
missinginfo + "\n_______________________ ______" +
"\nPlease re-enter and submit again!";
alert(missinginfo);
return false;
}
else return true;
}
// End -->
</script>
<form name=form onSubmit="return checkFields();">
<input type=hidden name=to value='you @ your domain . web'>
<input type=hidden name=subject value="Freedback">
<pre><div class=bookdesc >
Name: <input type=text name="name" size=30>
Address: <input type=text name="address" size=50>
Phone: <input type=text name="phone" size=10 >
E-mail: <input type=text name="from" size=30>
Credit Card: <input type=text name="card" size=16>
Web Site: <input type=text value="http://" name="website" size=30>
Comments: </div>
<textarea rows=3 cols=40 name="comments"></textarea >
<div class=bookdesc >SELECT THE BOOK TO BUY </div>
<BR>
<input type="checkbox" name="checkbox" value="$19.99" onChecked="update(this.for m)">HTML 4 for the World Wide Web Visual Quickstart Guide
<input type="checkbox" name="checkbox2" value="$45.0" onChecked="update(this.for m)">Design ing Web Usability : The Practice of Simplicity
<input type="checkbox" name="checkbox3" value="$39.95" onChecked="update(this.for m)">Javasc ript : The Definitive Guide
<input type="checkbox" name="checkbox4" value="$24.99" onChecked="update(this.for m)" >XML by Example
<input type="checkbox" name="checkbox5" value="$39.99" onChecked="update(this.for m)" >Dreamweaver 3 Hands-on Training
<input type="checkbox" name="checkbox6" value="$44.95"onChecked="u pdate(this .form)" >Web Development with Java Server Pages
<input type="checkbox" name="checkbox7" value="$39.99" onChecked="update(this.for m)">Dreamw eaver 3 Bible
<input type="checkbox" name="checkbox8" value="$39.99" onChecked="update(this.for m)" >Beginning Active Server Pages 3.0
<input type="checkbox" name="checkbox9" value="$49.99"onChecked="u pdate(this .form)" >Professional ASP XML
<input type="checkbox" name="checkbox10" value="$44.95"onChecked="u pdate(this .form)" >Dynamic Html : The Definitive Reference
Your total cost is ...............
<input type=image name="submit" src=submit.gif >
</pre>
</form>
but it doesn't work out quite right, I want to add the function that checks
that the phone textfeild are all numbers, the credit card feild are numbers with 16 characters.
I also want to add in the features that when the checkbox is ticked (the value that I set
for the checkbox is the price of each book), the value are added and the total is displayed
below as to how much it add up to.
And also in the name textfield that I have in my form, How can I put in the name of the
user from the cookie as its value.
all suggestions welcome.
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function checkFields() {
missinginfo = "";
if (document.form.name.value == "") {
missinginfo += "\n - Name";
}
if (document.form.address.val
missinginfo += "\n - Address";
}
if (document.form.phone.value
missinginfo += "\n - Phone";
}
if (document.form.card.value == "") {
missinginfo += "\n - Credit Card Number";
}
if ((document.form.from.value
(document.form.from.value.
(document.form.from.value.
missinginfo += "\n - Email address";
}
if ((document.form.website.va
(document.form.website.val
(document.form.website.val
missinginfo += "\n - Web site";
}
if(document.form.comments.
missinginfo += "\n - Comments";
}
if (missinginfo != "") {
missinginfo ="________________________
"You failed to correctly fill in your:\n" +
missinginfo + "\n_______________________
"\nPlease re-enter and submit again!";
alert(missinginfo);
return false;
}
else return true;
}
// End -->
</script>
<form name=form onSubmit="return checkFields();">
<input type=hidden name=to value='you @ your domain . web'>
<input type=hidden name=subject value="Freedback">
<pre><div class=bookdesc >
Name: <input type=text name="name" size=30>
Address: <input type=text name="address" size=50>
Phone: <input type=text name="phone" size=10 >
E-mail: <input type=text name="from" size=30>
Credit Card: <input type=text name="card" size=16>
Web Site: <input type=text value="http://" name="website" size=30>
Comments: </div>
<textarea rows=3 cols=40 name="comments"></textarea
<div class=bookdesc >SELECT THE BOOK TO BUY </div>
<BR>
<input type="checkbox" name="checkbox" value="$19.99" onChecked="update(this.for
<input type="checkbox" name="checkbox2" value="$45.0" onChecked="update(this.for
<input type="checkbox" name="checkbox3" value="$39.95" onChecked="update(this.for
<input type="checkbox" name="checkbox4" value="$24.99" onChecked="update(this.for
<input type="checkbox" name="checkbox5" value="$39.99" onChecked="update(this.for
<input type="checkbox" name="checkbox6" value="$44.95"onChecked="u
<input type="checkbox" name="checkbox7" value="$39.99" onChecked="update(this.for
<input type="checkbox" name="checkbox8" value="$39.99" onChecked="update(this.for
<input type="checkbox" name="checkbox9" value="$49.99"onChecked="u
<input type="checkbox" name="checkbox10" value="$44.95"onChecked="u
Your total cost is ...............
<input type=image name="submit" src=submit.gif >
</pre>
</form>
ASKER
Thank you for your suggestion,Nitinsontakke
however,In the name textfield that I have in my form, How can I put in the name of the
user from the cookie as its value. Do I use the document.cookie? I tried but I want
the value to be inside the text field... prefilled the field for the User.
the total works in IE but in netscape when you deselect everything it doesn't return to zero but it has negative value.
Anyway,do you think this function is a good idea?
where amount is the total, if it is, then where can I put it in the code.
function cent(amount) {
// returns the amount in the .99 format
return (amount == Math.floor(amount)) ? amount + '.00' : ( (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}
the function parseFloat explanation below is taken from the javascript NETSCAPE online documentation
Parameters
string
A string that represents the value you want to parse.
Description
The parseFloat function is a built-in JavaScript function.
parseFloat parses its argument, a string, and returns a floating point number. If it encounters a character other than a sign (+ or -), numeral (0-9), a decimal point, or an exponent, then it returns the value up to that point and ignores that character and all succeeding characters.
If the first character cannot be converted to a number, parseFloat returns "NaN".
For arithmetic purposes, the "NaN" value is not a number in any radix. You can call the isNaN function to determine if the result of parseFloat is "NaN". If "NaN" is passed on to arithmetic operations, the operation results will also be "NaN".
Examples
The following examples all return 3.14:
parseFloat("3.14")parseFlo at("314e-2 ")parseFlo at("0.0314 E+2")var x = "3.14"parseFloat(x)
however,In the name textfield that I have in my form, How can I put in the name of the
user from the cookie as its value. Do I use the document.cookie? I tried but I want
the value to be inside the text field... prefilled the field for the User.
the total works in IE but in netscape when you deselect everything it doesn't return to zero but it has negative value.
Anyway,do you think this function is a good idea?
where amount is the total, if it is, then where can I put it in the code.
function cent(amount) {
// returns the amount in the .99 format
return (amount == Math.floor(amount)) ? amount + '.00' : ( (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}
the function parseFloat explanation below is taken from the javascript NETSCAPE online documentation
Parameters
string
A string that represents the value you want to parse.
Description
The parseFloat function is a built-in JavaScript function.
parseFloat parses its argument, a string, and returns a floating point number. If it encounters a character other than a sign (+ or -), numeral (0-9), a decimal point, or an exponent, then it returns the value up to that point and ignores that character and all succeeding characters.
If the first character cannot be converted to a number, parseFloat returns "NaN".
For arithmetic purposes, the "NaN" value is not a number in any radix. You can call the isNaN function to determine if the result of parseFloat is "NaN". If "NaN" is passed on to arithmetic operations, the operation results will also be "NaN".
Examples
The following examples all return 3.14:
parseFloat("3.14")parseFlo
ASKER
function update(objCheckBox)
{
if(objCheckBox.checked)
document.someForm.total.va lue = parseFloat(document.someFo rm.total.v alue) + parseFloat(objCheckBox.val ue)
else
document.someForm.total.va lue = parseFloat(document.someFo rm.total.v alue) - parseFloat(objCheckBox.val ue)
document.someForm.total.va lue = '$' + cent(total);
// Is this a good idea?
}
{
if(objCheckBox.checked)
document.someForm.total.va
else
document.someForm.total.va
document.someForm.total.va
// Is this a good idea?
}
Difficult to comment actually. Any solution is good if it works as intended. Do play extensively with the code and better still tell someone else to play around to see if there are any unacceptable behaviour.
To answer your earlier question about user name, which server-side scripting you are using?
If yes, you may benefit from following code from MSDN:
<%
For Each strKey In Request.Cookies
Response.Write strKey & " = " & Request.Cookies(strKey) & "<BR>"
If Request.Cookies(strKey).Ha sKeys Then
For Each strSubKey In Request.Cookies(strKey)
Response.Write "->" & strKey & "(" & strSubKey & ") = " & _
Request.Cookies(strKey)(st rSubKey) & "<BR>"
Next
End If
Next
%>
Assuming you are using ASP and have cookie name "UserInfo" and which has sub key as "UserName", you can code as follows:
<Input Type="Text" Name="UserName" Value="<%=Request.Cookies( "UserInfo" )("UserNam e")%>" maxLength="30">
I have not tested the above code.
To answer your earlier question about user name, which server-side scripting you are using?
If yes, you may benefit from following code from MSDN:
<%
For Each strKey In Request.Cookies
Response.Write strKey & " = " & Request.Cookies(strKey) & "<BR>"
If Request.Cookies(strKey).Ha
For Each strSubKey In Request.Cookies(strKey)
Response.Write "->" & strKey & "(" & strSubKey & ") = " & _
Request.Cookies(strKey)(st
Next
End If
Next
%>
Assuming you are using ASP and have cookie name "UserInfo" and which has sub key as "UserName", you can code as follows:
<Input Type="Text" Name="UserName" Value="<%=Request.Cookies(
I have not tested the above code.
ASKER
NitinSontakke, I am not using a server side cookie but a client side. document.cookie, how can I put this in?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In summary, pending issues are as follows:
01. Check behaviour of parseFloat javaScript function
02. Find out a browser compatible solution to showing total
Suggestions are as follows:
01. Never give tag names as control name (form)
02. There is a maxLength attribute to Input tag which controls the maximum chars user can enter. Size attribute is related with display only.
03. The event on checkBox is onClick and not onChecked.
04. Never use currancy symbols in values used in calculation and it is strictly for human eyes and computer get confused with it.
Here is the code, not 100% tested.
<HTML>
<Head>
<Title>Document Title</Title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function checkFields() {
missinginfo = "";
if (document.someForm.name.va
missinginfo += "\n - Name";
}
if (document.someForm.address
missinginfo += "\n - Address";
}
if (document.someForm.phone.v
missinginfo += "\n - Phone";
}
if (document.someForm.card.va
missinginfo += "\n - Credit Card Number";
}
if ((document.someForm.from.v
(document.someForm.from.va
(document.someForm.from.va
missinginfo += "\n - Email address";
}
if ((document.someForm.websit
(document.someForm.website
(document.someForm.website
missinginfo += "\n - Web site";
}
if(document.someForm.comme
missinginfo += "\n - Comments";
}
if (missinginfo != "") {
missinginfo ="________________________
"You failed to correctly fill in your:\n" +
missinginfo + "\n_______________________
"\nPlease re-enter and submit again!";
alert(missinginfo);
return false;
}
else return true;
}
function update(objCheckBox)
{
if(objCheckBox.checked)
document.someForm.total.va
else
document.someForm.total.va
}
// End -->
</script>
</Head>
<Body>
<form name=someForm onSubmit="return checkFields();">
<input type=hidden name=to value='you @ your domain . web'>
<input type=hidden name=subject value="Freedback">
<pre><div class=bookdesc >
Name: <input type=text name="name" size=30 maxLength=30>
Address: <input type=text name="address" size=50 maxLength=50>
Phone: <input type=text name="phone" size=10 maxLength=10>
E-mail: <input type=text name="from" size=30 maxLength=30>
Credit Card: <input type=text name="card" size=16 maxLength=16>
Web Site: <input type=text value="http://" name="website" size=30 maxLength=30>
Comments: </div>
<textarea rows=3 cols=40 name="comments"></textarea
<div class=bookdesc >SELECT THE BOOK TO BUY </div>
<BR>
<input type="checkbox" name="checkbox" value="19.99" onClick="update(this)">HTM
Wide Web Visual Quickstart Guide
<input type="checkbox" name="checkbox2" value="45.0" onClick="update(this)">Des
: The Practice of Simplicity
<input type="checkbox" name="checkbox3" value="39.95" onClick="update(this)">Jav
Definitive Guide
<input type="checkbox" name="checkbox4" value="24.99" onClick="update(this)" >XML by Example
<input type="checkbox" name="checkbox5" value="39.99" onClick="update(this)" >Dreamweaver 3
Hands-on Training
<input type="checkbox" name="checkbox6" value="44.95"onClick="upda
with Java Server Pages
<input type="checkbox" name="checkbox7" value="39.99" onClick="update(this)">Dre
<input type="checkbox" name="checkbox8" value="39.99" onClick="update(this)" >Beginning Active
Server Pages 3.0
<input type="checkbox" name="checkbox9" value="49.99"onClick="upda
XML
<input type="checkbox" name="checkbox10" value="44.95"onClick="upda
The Definitive Reference
Your total cost is ...............$<Input Type=Text Value="0" Name="total" ReadOnly>
<input type=image name="submit" src=submit.gif >
</pre>
</form>
</Body>
</HTML>