Solved

java script works for IE not firefox

Posted on 2011-03-02
28
612 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
  • 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
 
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 500 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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 how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now