-3

I have a text input where users are able to write addresses, I want to trigger a javascript function when a number is typed in the text input, how can I achieve it?

EDIT: What I need is to know when a number has been typed, onkeyup via jquery works like a charm, this is what I did:

$("#q").keyup((e) => {

        var value = $("#q").val();

        var regex = new RegExp("[a-zA-Z0-9_ ]\\d+");

        var succeded = regex.test(value);

        ...
SaraFlower
  • 755
  • 1
  • 8
  • 15

4 Answers4

1

You can simply do:

$("input").keyup(function(){
    $("input").css("background-color", "pink");
});

Source

Amrinder Singh
  • 5,300
  • 12
  • 46
  • 88
1

you just need to use build in KeyEvents, like (keyDown, keyUp, keyPress , etc.). You can find a lot of information about it. KeyboardEvent mozilla

  1. Create html input, and give unique ID.
  2. Form JS code take this element.
  3. Then just bind event handler to target element.

    ["YOUR_ELEMENT"].addEventListener("keypress", function(e) { console.log(e.value);}, false);

Taras
  • 36
  • 1
0
$('.js-input').on('keydown', function(e) {
    console.log('pressing:', e.keyCode);
});

<input type="text" class="js-input">

DEMO

This will get you started

Dejan.S
  • 18,571
  • 22
  • 69
  • 112
0

This will do your job:

$('#tbInput').keypress(function(event){

if(event.which != 8 && isNaN(String.fromCharCode(event.which))){
}
else
{
//Your function that you want to trigger.
alert("You pressed a numberic key: " + event.which);
}});

You can test it here: JSFIDDLE DEMO