0

I am new to javascript. What I am trying to do is to make a password validation system and get user focus on the input.

my main question is can you use more than one function for onkeyup?

example: <input onkeyup="validatepassword(),onfocus()">

The validatepassword function is to check user input e.g. password length.

The onfocus function is to get user focus on the input.

  • I'm getting the feeling you are approaching this the wrong way somehow. First, why keyup? Question A: what if the input is done without any keypresses (for instance rightclick and paste, dragging text into the field, autocomplete, etc)? Question B: Isn't it annoying to the user to get some sort of validation while they are still typing ("yes I know this isn't valid because I'm _not done typing yet_ so can you please chill, website")? Second, what do you mean by "get user focus on the input"? If your field receives a keyup event, it must already have focus, no? – CherryDT Aug 01 '21 at 13:27
  • Does this answer your question? [How to call multiple JavaScript functions in onclick event?](https://stackoverflow.com/questions/3910736/how-to-call-multiple-javascript-functions-in-onclick-event) – CherryDT Aug 01 '21 at 13:31

4 Answers4

1

If you want to bind more than one listener to the same event, you can do so using EventTarget.addEventListener()

function validatepassword (event) {
  console.log('validatepassword');
}

function onfocus (event) {
  console.log('onfocus');
}

const input = document.querySelector('input');

input.addEventListener('keyup', validatepassword);
input.addEventListener('keyup', onfocus);
<input>
jsejcksn
  • 27,667
  • 4
  • 38
  • 62
0

You could simply call those functions from another function :

<input onkeyup="onKeyUpActions()"> 

function onKeyUpActions() {
validatepassword();
onfocus();
}


tsamridh86
  • 1,480
  • 11
  • 23
0

Why not simply create a new function that calls all of them? Something like:

function myOnKeyUp(e) {
    validatePassword(e);
    onfocus(e);
}

using <input onkeyup="myOnKeyUp(event)">.

Kelvin Schoofs
  • 8,323
  • 1
  • 12
  • 31
0

You can try to replace the "," by ";". Must be works.

<input type="text" onkeyup="test1();test2()">

<script>
   function test1() {
     console.log('t1')
   }

   function test2() {
     console.log('t2')
   }
 </script>
GiovannyLucas
  • 129
  • 1
  • 3
  • 1
    It works with `,` too (the comma operator), but the better solution would be to use `addEventListener` – CherryDT Aug 01 '21 at 13:23