Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

input field

Status
Not open for further replies.

novice2004

Programmer
Feb 2, 2004
62
US
I have ZipCode input field.
Is there a way when user tries to enter letter or any other char besides number nothing get displayed in the field?
I don't want to display alert box.

Thank you.
 
Like this?

Code:
<input type="text" onkeyup='this.onchange();' onchange='this.value=this.value.replace(/[^\d]*/gi,"");' />

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
This should work, I think.

In the head:
Code:
<script type="text/javascript">
<!--
function repressNonNumbers(event)
{
  var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  return (keycode <= 57 && keycode >= 48);
}
// -->
</script>
Your element:
Code:
<input type="text" maxlength="5" onkeydown="return repressNonNumbers(event);">

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
cb-

I like your approach better, the solution I provided wasn't even original. I had originally tried something similar to your solution, but couldn't get it to work. This worked, however:

Code:
var kc = 0;
function repressNonNumbers(event)
{
  return (kc <= 57 && kc >= 48) || kc == 8;
}

Code:
<input type="text" maxlength="5" onkeydown="kc = event.keyCode;" onkeypress="return repressNonNumbers(event);">

However, there's still the problem of someone holding down SHIFT and pressing a numeric key. Any further ideas?

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
Here's a much less sophisticated, but equally reliable solution:

Code:
<script language="javascript">
function chars(string,Chars) {
 var returnstring = '';
 for (var i = 0; i < string.length; i++) {
  if (Chars.indexOf(string.charAt(i)) > -1)
   returnstring = returnstring + string.charAt(i);
  }
 return returnstring;
}
</script>

<form>
 <input type="text" name="fld" onKeyUp="this.value=chars(this.value,'1234567890');">
</form>

Joe
 
Number+shift is a different key than number, i.e.
Code:
number       1 2 3 4 5
shift+number ! @ # $ %

This suppresses other characters too.

joepeacock:
Works, definitely. Elegant, as it allows for characters other than numbers. However, it requires the value to be reset every time.

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
chessbot;

yes, but with the solution provided (keypress), it fires when each key is pressed individually.

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
Oh.

How about with onkeyup?

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top