Fahid Posted February 18, 2006 Share Posted February 18, 2006 HI Guys!I wanted to make an input box to accept only numeric (int) values, to do that I wrote following function.[code]function is_int(Key) {/* 48 is ASCII code for 0 and 57 is for 9 */ //alert(Key); for(i=48;i<=57;i++){ if(Key == i) return true; } return false;}[/code]and my input box look like this[code]<input type="text" name="quantity" id="quantity" size="30" maxlength="3" value="1" onkeypress="return is_int(event.keyCode);" />[/code]Now! when I use IE6 or Opera, everythings just going perfect, But when I use Firefox, I got a problem.Firefox make it impossible to type anything in the input box, not even integers.Infact Firefox return [b]0[/b], not the actuall ASCII code of the key that was pressed.[!--sizeo:6--][span style=\"font-size:24pt;line-height:100%\"][!--/sizeo--]Any Idea how to get it right ?[!--sizec--][/span][!--/sizec--] Quote Link to comment Share on other sites More sharing options...
ryanlwh Posted February 20, 2006 Share Posted February 20, 2006 stumbled upon this line of code[code]key = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;[/code]That should do it. Quote Link to comment Share on other sites More sharing options...
Fahid Posted February 21, 2006 Author Share Posted February 21, 2006 Thanks for response ryan, but I still can't find out, how exactly to do it.this is a sample page which uses the script[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><script type="text/javascript">/*This funtion helps to make an input-box to only accept integers*/function is_int(Key) {/* 48 is ASCII code for 0 and 57 is for 9 */ /* ryanlwh's code Key = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode; */ //alert(Key); if(Key == 13) return true; for(i=48;i<=57;i++){ if(Key == i) return true; } return false;}</script><title>Numeric Only TextBox</title></head><body><input type="text" onkeypress="return is_int(event.keyCode);" /></body></html>[/code]When I use [code]<input type="text" onkeypress="return is_int(event.keyCode);" />[/code]IE and Opera works just as I want, but Firefox doesn't.AndWhen I use [code]<input type="text" onkeypress="return is_int(event.charCode);" />[/code]Firefox works, but neither IE nor OperaOKI changed my javascript to this[code]function is_int(empty) { // in Firefox navigator.appName returns "Netscape". if(navigator.appName == "Netscape") Key = event.charCode; else Key = event.keyCode; alert(Key); // removed the rest of code for test return false;}[/code]Still IE and Opera respond well, but not Firefox. Quote Link to comment Share on other sites More sharing options...
ryanlwh Posted February 22, 2006 Share Posted February 22, 2006 this is what i'm using. i forgot how i googled it but it worked for me[!--html--][div class=\'htmltop\']HTML[/div][div class=\'htmlmain\'][!--html1--]<[color=blue]input type[/color]="[color=orange]text[/color]" onkeypress="[color=orange]return is_int(event);[/color]" />[!--html2--][/div][!--html3--][code]function is_int(event) { var Key = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode; // the following is copied and pasted from yours if(Key == 13) return true; for(i=48;i<=57;i++){ if(Key == i) return true; } return false;}[/code]may i suggest another way to check the keycode[code]function is_int(event){ var Key = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode; if(Key==13 || (Key >= 48 && Key <=57)) return true; else return false;}[/code] Quote Link to comment Share on other sites More sharing options...
Fahid Posted February 23, 2006 Author Share Posted February 23, 2006 Thanks a Lot [b]ryanlwh[/b]Now It's working fine.Thank you very much. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.