checkbox problems? 
July 23rd, 2005, 08:52 PM
| | | |
I have a shopping cart that I am having a few problems with. I am
mostly a flash developer with basic if not little javascript knowledge,
I need for my checkout to have a checkbox that if clicked on will
populate my shipping addresses with my billing addresses, I have this
somewhat working with a code I got off of here thats like this
<script>
function copy(checked, source, target) {
if (checked) {
target.value = source.value
} else {
target.value = '';
}
}
</script>
//then like this in the form
<input type=checkbox onClick="copy(this.checked, this.form.bname,
this.form.sname)">
but the problem with that is that I have to put a checkbox with its own
code on each part of the form. I would like to have one that would
then populate the whole form. The other question I have is I need to
have a validate script from the form, the fields are
Name-text
address-text
city-text
state-select
zip-text
country-selected US
Phone-text
email-text
credit card number-text- just need it to check if its numbers and over
12
Any help is greatly appreciated, thanks in advance | 
July 23rd, 2005, 08:53 PM
| | | | re: checkbox problems?
No ideas? | 
July 23rd, 2005, 08:53 PM
| | | | re: checkbox problems?
Rabel wrote:
[color=blue]
> No ideas?
>[/color]
I think the problem may be that you didn't actually ask a question. This
isn't a place to get people to write your code for you from scratch, it
is a place to ask questions when you get stuck. Try showing us what you
have tried so far, say what you expected it to do and what it actually
does and then maybe someone will suggest where you are going wrong.
[color=blue]
> I would like to have one that would then populate the whole form.
> The other question I have is I need to have a validate script from
> the form[/color]
On the face of it you stated that you wanted to do something pretty
obvious so my response would be 'so do it'.
See also http://www.catb.org/~esr/faqs/smart-questions.html | 
July 23rd, 2005, 08:55 PM
| | | | re: checkbox problems?
You are an idiot duncan, I did ask a question, and I was only asking
for help I believe I said
'Any HELP is greatly appreciated, thanks in advance'
but you decided that I knew what I was doing and suggested 'so do it'
well thanks man you were a lot of help. Who sits there and complains
about someones question anyway, get a life.
For everyone else out there who may have the same problem this is what
I used to solve it
<script>
function doit(obj,name,address,cityv,statev,zipcode)
{
if(obj.checked)
{
document.frm.elements[name].value = document.frm.bname.value
document.frm.elements[address].value = document.frm.baddr1.value
document.frm.elements[cityv].value = document.frm.bcity.value
document.frm.elements[statev].value = document.frm.bstate.value
document.frm.elements[zipcode].value = document.frm.bzip.value
}
else
{
document.frm.elements[name].value = ""
document.frm.elements[address].value = ""
document.frm.elements[cityv].value = ""
document.frm.elements[statev].value = ""
document.frm.elements[zipcode].value = ""
}
}
function validate()
{
var frm = document.frm
if(frm.bname.value == "" )
{
alert("Please enter valid billing first and last name");
return false;
}
if(frm.sname.value == "" )
{
alert("Please enter valid shipping first and last name");
return false;
}
if(frm.baddr1.value == "" )
{
alert("Please enter valid billing address");
return false;
}
if(frm.saddr1.value == "" )
{
alert("Please enter valid shipping address");
return false;
}
if(frm.bcity.value == "")
{
alert("Please enter valid billing city");
return false;
}
if(frm.scity.value == "")
{
alert("Please enter valid shipping city");
return false;
}
if(isNaN(frm.bzip.value) || (frm.bzip.value.length != 5 ))
{
alert("Please enter a valid zip code");
return false;
}
if(isNaN(frm.szip.value) || (frm.szip.value.length != 5 ))
{
alert("Please enter a valid zip code");
return false;
}
if(isNaN(frm.cardnumber.value) || frm.cardnumber.value.length <13)
{
alert("Please enter valid creditcard number");
return false;
}
if(isNaN(frm.phone.value) || frm.phone.value.length <10)
{
alert("Please enter a valid telephone number");
return false;
}
if(trimit(frm.email.value) != "")
{
return emailTest(frm.email)
}
}
function emailTest(obj){
reg=
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
if (!reg.test(obj.value) ){
alert('Invalid email address')
obj.focus()
return false;
}
}
function trimit(strInput)
{
return strInput.replace(/^\s*/,"").replace(/\s*$/,"");
}
</script>
//in the form tag
onsubmit="return validate()
//then on the checkbox
<input type = "checkbox" name = "chk" onclick =
"doit(this,'sname','saddr1','scity','sstate','szip ');">
Hope that helps | 
July 23rd, 2005, 08:56 PM
| | | | re: checkbox problems?
JRS: In article <1116528773.238123.173150@f14g2000cwb.googlegroups .com>
, dated Thu, 19 May 2005 11:52:53, seen in news:comp.lang.javascript,
Rabel <Randy@Creativeness.com> posted :
[color=blue]
>if(isNaN(frm.phone.value) || frm.phone.value.length <10)
>{
>alert("Please enter a valid telephone number");
>return false;
>}[/color]
Please use proper quoting and attributions on Usenet :-
For proper quoting when using Google for News :-
Keith Thompson wrote in comp.lang.c, message ID
<lnwtuhfy7d.fsf@nuthaus.mib.org> :-
If you want to post a followup via groups.google.com, don't use
the "Reply" link at the bottom of the article. Click on "show
options" at the top of the article, then click on the "Reply" at
the bottom of the article headers.
That's rather crude validation - it will accept 0xFeedBeef and
0xDeadFade, for example. Similarly for other answers.
Better to test with a RegExp such as /^\d{10,}$/.test(frm.phone.value)
which calls explicitly for 10 or more decimal digits. OTOH, it would be
sensible to permit customary punctuation.
See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.
Replacing everything like
if(frm.bcity.value == "")
{
alert("Please enter valid billing city");
return false;
with like
if (Bad(frm.bcity, "Please enter valid billing city")
return false;
and a function like
function Bad(C, S) { var B = C.value == ""
if (B) alert(S)
return B }
would make your code more readable, more maintainable, and probably
shorter.
The function in your first article,
function copy(checked, source, target) {
if (checked) {
target.value = source.value
} else {
target.value = '';
}
}
could be written as
function copy(checked, source, target) {
target.value = checked ? source.value : '' }
Code presented for others to read should be indented to show intended
structure.
Your code serves well as an example not to be followed.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links. | 
July 23rd, 2005, 09:01 PM
| | | | re: checkbox problems?
Rabel wrote:
[color=blue]
> You are an idiot duncan,[/color]
I don't like several people here either, however with me
there is enough courtesy left to refrain from such.
[color=blue]
> I did ask a question, and I was only asking for help[/color]
Does not matter anymore. Go away, please.
[color=blue]
> [...]
> For everyone else out there who may have the same problem this is what
> I used to solve it[/color]
It is not Valid (X)HTML, it is highly inefficient and it is flawed.
PointedEars | 
July 23rd, 2005, 09:15 PM
| | | | re: checkbox problems?
oh well it works for what i need it to do |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|