Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

java script works for IE not firefox

Posted on 2011-03-02
28
Medium Priority
?
623 Views
Last Modified: 2013-11-26
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 !!!!!!
0
Comment
Question by:AhmedHindy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 12
  • +2
28 Comments
 
LVL 16

Expert Comment

by:Swapnil Piparia
ID: 35016859
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
 
LVL 82

Expert Comment

by:leakim971
ID: 35016882
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35017310
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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 4

Author Comment

by:AhmedHindy
ID: 35024839
i tried the code above
this code works like charm on IE but not working at firefox
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35024945
What does the console say?
0
 
LVL 4

Author Comment

by:AhmedHindy
ID: 35024955
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
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 2000 total points
ID: 35024986
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
 
LVL 4

Author Comment

by:AhmedHindy
ID: 35025171
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35025356
I do not know. The code I posted DOES work on Fx. Perhaps some other problem?

Did you check the console for errors?
0
 
LVL 4

Author Comment

by:AhmedHindy
ID: 35025530
here is the error  snap shot of console
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35025816
Please remove event from the
onkeypress= "fncInputNumericValuesOnly(event)"
0
 
LVL 1

Expert Comment

by:goodorbad
ID: 35033713
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35033788
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
 
LVL 4

Author Comment

by:AhmedHindy
ID: 35080316
i tried goodor bad code and here is what i got
Untitled.png
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35080409
We cannot help you if you do not post the code generated in the browser
0
 
LVL 4

Author Comment

by:AhmedHindy
ID: 35080509
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35080684
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
 
LVL 4

Author Comment

by:AhmedHindy
ID: 35083632
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
 
LVL 4

Author Comment

by:AhmedHindy
ID: 35083724
the previous problems has been solved  but i am facing another problem

 window.history.forward(1);
is not working on firefox
any suggestions ?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35083769
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
 
LVL 4

Author Comment

by:AhmedHindy
ID: 35093008
when i used  location.replace(url) the page has been  continously reloaded
did i do some thong wrong ?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35093544
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
 
LVL 4

Author Comment

by:AhmedHindy
ID: 35120317
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35120337
Yes: do NOT try to disable browser buttons
0
 
LVL 4

Author Comment

by:AhmedHindy
ID: 35120341
why do you advice me not to disable browser buttons ?
0
 
LVL 4

Author Comment

by:AhmedHindy
ID: 35120772
if i disabled browser buttons .. what is the other alternative ?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35120815
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
 
LVL 4

Author Comment

by:AhmedHindy
ID: 35120835
Thanks :)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question