java script works for IE not firefox

hello ,
this code works well in IE not working at all with firefox
here is the code :



<asp:TextBox ID="Cash" runat="server" Width="160px" onpaste="return false" oncut="return false"
                        oncopy="return false" TabIndex="2" Enabled="False" MaxLength="7"  onkeypress= "fncInputNumericValuesOnly('txtQty')" ></asp:TextBox>

java script

function fncInputNumericValuesOnly()      
{
if(!(event.keyCode==45||event.keyCode==46||event.keyCode==48||event.keyCode==49||event.keyCode==50||event.keyCode==51||event.keyCode==52||event.keyCode==53||event.keyCode==54||event.keyCode==55||event.keyCode==56||event.keyCode==57))
      {            
            event.returnValue=false;      
      }

               
}

thsi code is let the user to enter numeric values only in text box

please heeeeeeelp !!!!!!
LVL 4
AhmedHindyAsked:
Who is Participating?
 
Michel PlungjanIT ExpertCommented:
This one works
http://stackoverflow.com/questions/469357/html-text-input-allow-only-numeric-input/469419#469419


<script>
function fncInputNumericValuesOnly(evt)  {
  var theEvent = evt || window.event;
  var key = theEvent.keyCode || theEvent.which;
  key = String.fromCharCode( key );
  var regex = /[0-9]|\./; // remove the |\. if you want to disallow decimal point
  if( !regex.test(key) ) {
    theEvent.returnValue = false;
    theEvent.preventDefault();
  }
}

</script>
<input onkeypress= "fncInputNumericValuesOnly(event)" />

Open in new window

0
 
SwapnilSoftware ArchitectCommented:
Hi AhmedHindy,

HTML
<asp:TextBox ID="Cash" runat="server" Width="160px" onpaste="return false" oncut="return false"
                        oncopy="return false" TabIndex="2" Enabled="False" MaxLength="7"  onkeypress= "fncInputNumericValuesOnly()" ></asp:TextBox>

Open in new window


you are passing parameter to fncInputNumericValuesOnlyI() however you don't have parameter in function definition. So I removed in above code.

Javascript
 function fncInputNumericValuesOnly()      
 {
var Key = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
if(!(Key==45||Key==46||Key==48||Key==49||Key==50||Key==51||Key==52||Key==53||Key==54||Key==55||Key==56||Key==57))
      {     
             return false;       
 }
}

Open in new window


Thanks,
netswap
0
 
leakim971PluritechnicianCommented:
Firefox use event.which event.keyCode instead : https://developer.mozilla.org/en/DOM/event.which
Check Which key has been pressed? section here : http://www.quirksmode.org/js/events_properties.html
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Michel PlungjanIT ExpertCommented:
netswqp, you need to pass the event in the handler:

onkeypress= "fncInputNumericValuesOnly(event)"

function fncInputNumericValuesOnly(e)  {
  var key = (e)?e.which:event.keyCode;
  return key==45 || key==46|| key==46|| key==47|| key==48|| key==49|| key==50|| key==51|| key==52|| key==53|| key==54|| key==55|| key==56|| key==57;
}
0
 
AhmedHindyAuthor Commented:
i tried the code above
this code works like charm on IE but not working at firefox
0
 
Michel PlungjanIT ExpertCommented:
What does the console say?
0
 
AhmedHindyAuthor Commented:
this function is to allow user only to write only numbers
in IE user can't insert leters but when i use firefox user can insert leters
0
 
AhmedHindyAuthor Commented:
Thank You so much for your support but the same problem still exist
can something wrong with firefix or it is n't working properly ?
i am developing website on machine (don't have forefox) and browse the website on another machine (has  firefox /windows 7 )
can this be the problem ?



0
 
Michel PlungjanIT ExpertCommented:
I do not know. The code I posted DOES work on Fx. Perhaps some other problem?

Did you check the console for errors?
0
 
AhmedHindyAuthor Commented:
here is the error  snap shot of console
0
 
Michel PlungjanIT ExpertCommented:
Please remove event from the
onkeypress= "fncInputNumericValuesOnly(event)"
0
 
goodorbadCommented:
I hope this code will work, because it work fine on my dev environment

 
function fncInputNumericValuesOnly() {

    var _IfIE = (window.navigator.userAgent.indexOf("MSIE") > 0);
    var _KeyCode = (_IfIE) ? event.keyCode : event.charCode;
    if (!(_KeyCode == 45 || _KeyCode == 46 || _KeyCode == 48
            || _KeyCode == 49 || _KeyCode == 50 || _KeyCode == 51
            || _KeyCode == 52 || _KeyCode == 53 || _KeyCode == 54 
            || _KeyCode == 55 || _KeyCode == 56 || _KeyCode == 57)) {
        event.returnValue = false;
    }
    else
        event.returnValue = true;
}

Open in new window


If still error come, show your error description.
0
 
Michel PlungjanIT ExpertCommented:
Goodorbad that seems to be identical in principle to what has already been posted. My code also works perfectly on my pc. I need to see what asp does to the code BEFORE it hits the browser. A URL wouldd be great
0
 
AhmedHindyAuthor Commented:
i tried goodor bad code and here is what i got
Untitled.png
0
 
Michel PlungjanIT ExpertCommented:
We cannot help you if you do not post the code generated in the browser
0
 
AhmedHindyAuthor Commented:
here is the code generated by browser :

<script>  function FilterNumeric(){ var re; var ch=String.fromCharCode(event.keyCode);if (event.keyCode<32){return;};if( (event.keyCode<=57)&&(event.keyCode>=48)){if (!event.shiftKey){return;}}event.returnValue=false;}</script>
0
 
Michel PlungjanIT ExpertCommented:
It is really missing the event.

Can you post what this generates, especially for the input field - Why is Enabled="False" ?

notice the bold event:


<asp:TextBox ID="Cash" runat="server" Width="160px" onpaste="return false" oncut="return false"
oncopy="return false" TabIndex="2" Enabled="False" MaxLength="7"  
onkeypress= "fncInputNumericValuesOnly(event)" ></asp:TextBox>
<script>
function fncInputNumericValuesOnly(evt)  {
  var theEvent = evt || window.event;
  var key = theEvent.keyCode || theEvent.which;
  key = String.fromCharCode( key );
  var regex = /[0-9]|\./; // remove the |\. if you want to disallow decimal point
  if( !regex.test(key) ) {
    theEvent.returnValue = false;
    theEvent.preventDefault();
  }
}

</script>

Open in new window

0
 
AhmedHindyAuthor Commented:
i used the following code and it worked in firefox

<asp:TextBox ID="Cash" runat="server" Width="160px" onpaste="return false" oncut="return false"
                        oncopy="return false" TabIndex="2" Enabled="False" MaxLength="7"   onkeyup="isNumber(this)"  ></asp:TextBox>


function isNumber(field) {
var re = /^[0-9-'.'-',']*$/;
if (!re.test(field.value)) {
alert("¿¿ ¿¿¿¿ ¿¿¿¿ ¿¿¿¿¿ ¿¿¿");
field.value = field.value.replace(/[^0-9-'.'-',']/g,"");
}
}

0
 
AhmedHindyAuthor Commented:
the previous problems has been solved  but i am facing another problem

 window.history.forward(1);
is not working on firefox
any suggestions ?
0
 
Michel PlungjanIT ExpertCommented:
No. why would you want to break the back button?

do a location.replace(url) if you do not want people to go back to the page
0
 
AhmedHindyAuthor Commented:
when i used  location.replace(url) the page has been  continously reloaded
did i do some thong wrong ?
0
 
Michel PlungjanIT ExpertCommented:
Yes - when the people click next page, you can instead of loading the page use
<a href="nextpage.html" onclick="location.replace(this.href); return false">Next page</a>

then they cannot go back

But perhaps I need to know more about why you use history.forward at all
0
 
AhmedHindyAuthor Commented:
i want to disable back , forward button in browser
window.history.forward(1);
works fine in IE
but not working at all with firefox .. any suggestions ??????????
0
 
Michel PlungjanIT ExpertCommented:
Yes: do NOT try to disable browser buttons
0
 
AhmedHindyAuthor Commented:
why do you advice me not to disable browser buttons ?
0
 
AhmedHindyAuthor Commented:
if i disabled browser buttons .. what is the other alternative ?
0
 
Michel PlungjanIT ExpertCommented:
Firstly you CANNOT reliably do it as you now have seen
Secondly your site needs to be able to handle a back button
Thirdly the user will not appreciate you break the browser interface just for some server technical reason. it is bad practise to not handle such things without breaking the browser
0
 
AhmedHindyAuthor Commented:
Thanks :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.