Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Web Design & Development
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming gpu hard drive hardware hdmi internet laptop malware memory monitor motherboard netgear network printer problem ram registry router security slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
Solved: Javascript Query - Functions and OnClick

Reply  
Thread Tools
Swanny86uk's Avatar
Computer Specs
Member with 52 posts.
 
Join Date: Dec 2006
Experience: Intermediate
16-Jun-2008, 08:46 AM #1
Solved: Javascript Query - Functions and OnClick
Hi,

Doing a javascript course atm and slightly confused with a certain javascript event handler.

I am using regex to validate both a postcode and a "store" card. I have seperate functions for these validate_Postcard and validate_Fidelity. Is it possible to execute more than one function with the onclick event handler ??
I currently have just one function on the 'onclick' (see below)

onclick="return validate_Postcode(document.form1.elements[5]);"/>

Thanks

Marc
TheRobatron's Avatar
Computer Specs
Senior Member with 553 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
16-Jun-2008, 03:33 PM #2
Let me know if I haven't understood you, but to call two functions for one event, you will have to create a new function that calls both functions from within it.
tomdkat's Avatar
Computer Specs
Distinguished Member with 7,127 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
16-Jun-2008, 08:49 PM #3
Quote:
Originally Posted by TheRobatron View Post
Let me know if I haven't understood you, but to call two functions for one event, you will have to create a new function that calls both functions from within it.
Nope, he can call multiple functions with the same handler:

Code:
onClick="funcA(); funcB();"
Here is a test case:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

  <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
  <title>JavaScript test</title>


  <script type="text/javascript">
function funcA() {
alert("Function A has been called!");
}
function funcB() {
alert("Function B has been called!");
}
  </script>
</head>
<body>

<p>Here is a <a href="#" onclick="funcA(); funcB();">link</a>
you can click</p>

</body>
</html>
And the functions will be called in the order specified in the specification of the onClick event handler.

Peace...
Swanny86uk's Avatar
Computer Specs
Member with 52 posts.
 
Join Date: Dec 2006
Experience: Intermediate
17-Jun-2008, 05:49 AM #4
Excellent, i'll give that a try.
Swanny86uk's Avatar
Computer Specs
Member with 52 posts.
 
Join Date: Dec 2006
Experience: Intermediate
17-Jun-2008, 06:42 AM #5
Now that all made sense so i've tried it and it doesn't seem to work, so i'm thinking its my code. My functions and onclick code is below.

Functions

function validate_Postcode(node) {
var re = /^[A-Za-z]{1,2}\d{1,2}[A-Za-z]? \d[A-Za-z]{2}$/;
var str = node.value;
if (re.test(str))
return true;
else {
alert("The Postcode " + node.value + " is not a valid UK Postcode, please try again.");
return false;
}
}

function validate_Fidelity(node) {
var re = /^[A-Za-z]\w{12}\d[\?!&@]$/;
var str = node.value;
if (re.test(str))
return true;
else {
alert("The Fidelity Card number " + node.value + " is not a valid account number, please try again.");
return false;
}
}

Onclick code

onclick="return validate_Postcode(document.form1.elements[5]); return validate_Fidelity(document.form1.elements[8]);"

The postcode validator works but the Fidelity doesn't wanna play.
MMJ's Avatar
MMJ MMJ is offline
Senior Member with 3,637 posts.
 
Join Date: Oct 2006
17-Jun-2008, 10:01 AM #6
return tells the code to stop running and if in a function, to return the value.

So remove the first return:
Code:
onclick="validate_Postcode(document.form1.elements[5]); return validate_Fidelity(document.form1.elements[8]);"
Swanny86uk's Avatar
Computer Specs
Member with 52 posts.
 
Join Date: Dec 2006
Experience: Intermediate
17-Jun-2008, 10:17 AM #7
Yeh that one worked, thanks alot !

2 Days of searching and going grey and its one word, typical eh?!

Thanks again.
Reply

Tags
javascript

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 02:19 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.