# Solved: Javascript Query - Functions and OnClick



## Swanny86uk (Dec 1, 2006)

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 (Oct 25, 2007)

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 (May 6, 2006)

TheRobatron said:


> 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:


```
onClick="funcA(); funcB();"
```
Here is a test case:

```
<!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="#" [b]onclick="funcA(); funcB();[/b]">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 (Dec 1, 2006)

Excellent, i'll give that a try.


----------



## Swanny86uk (Dec 1, 2006)

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 (Oct 15, 2006)

*return* tells the code to stop running and if in a function, to return the value.

So remove the first return:

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


----------



## Swanny86uk (Dec 1, 2006)

Yeh that one worked, thanks alot !

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

Thanks again.


----------

