• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1475
  • Last Modified:

onkeyup show alert or popup

Is there a way with the existing code below, to dispaly a popup window or alert box on key up as well?

<input type="text" name="num" id="num" size="5" value="0" onKeyup="if(this.value.match(/[^0-9\-]/))this.value=this.value.replace(/[^0-9\-]/g,'')'">

Right now it makes sire only numeric values are entered.


0
JuniorBee
Asked:
JuniorBee
  • 4
1 Solution
 
thirdCommented:
sure,

<input type="text" name="num" id="num" size="5" value="0" onKeyup="if(this.value.match(/[^0-9\-]/)){this.value=this.value.replace(/[^0-9\-]/g,'');alert(this.value)}">
0
 
thirdCommented:
it will be neater and much easier to maintain if you put it in a function,

<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Author: Third Santor</title>
<script>
  function trapKey(obj){
    if(obj.value.match(/[^0-9\-]/)){
      obj.value = obj.value.replace(/[^0-9\-]/g,'');
      alert(obj.value);
    }
  }
</script>
</head>
<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">
<form name="form1" method=post action="" onsubmit="">
<input type="text" name="num" id="num" size="5" value="0" onKeyup="trapKey(this);">
</form>
</body>
</html>
0
 
JuniorBeeAuthor Commented:
Thx,
What you gave me pops up an alert when an invalid character is entered.  I guess I should have clarified better.  I want it to pop up and say something after a valid number is entered.  Something like after they type in their number(s) and the keyup an alert box will say "Entering this value will change your price".
0
 
thirdCommented:
you may want to alert a message telling the user to input numeric keys only.

function trapKey(obj){
    if(obj.value.match(/[^0-9\-]/)){
      alert('Invalid key! Please use numbers only...');
      obj.value = obj.value.replace(/[^0-9\-]/g,'');      
    }
  }
0
 
thirdCommented:
ok then just change it to

function trapKey(obj){
    if(obj.value.match(/[^0-9\-]/)){      
      obj.value = obj.value.replace(/[^0-9\-]/g,'');      
    }
    else{
      alert('Entering this value will change your price.');
    }
  }
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now