0

I have this function:

function validar(e) {
  var xx = 1;
  tecla = (document.all) ? e.keyCode : e.which;     // 2
  if (tecla == 8) return true;                      // 3
  patron = /[A-Za-z\s]/;                            // 4
  te = String.fromCharCode(tecla);                  // 5

  var valorNumerico = Number(te); 
  if (te > 1 && te < 11) {
    alert('Calificación:' + valorNumerico)
  } else {
    alert('La Calificación debe estar entre 2 y 10  - ' + valorNumerico)
  }
}

The goal its catch the input the user and validate the range into 2 to 10

But, shows me NaN

I tried valorNumerico = parseInt(te);

Shows me NaN

I tried valorNumerico = Integer.parseInt(te);

Shows me NaN

So, how I can convert to integer? thanks I used a function in javascript into asp.net

Jeroen Heier
  • 3,520
  • 15
  • 31
  • 32
David Ortega
  • 27
  • 1
  • 6
  • how are you declaring `te`? and what is `tecla`? – Jon Apr 28 '17 at 15:35
  • What is the value of `te`? Not every string value can be converted to a number. What's your ultimate goal here? – Felix Kling Apr 28 '17 at 15:35
  • Seems like te doesn't contain a number – the_lotus Apr 28 '17 at 15:37
  • function validar(e) { var xx = 1; tecla = (document.all) ? e.keyCode : e.which; // 2 if (tecla == 8) return true; // 3 patron = /[A-Za-z\s]/; // 4 te = String.fromCharCode(tecla); // 5 var valorNumerico = Number(te); if (te > 1 && te < 11) { alert('Calificación:' + valorNumerico) } else { alert('La Calificación debe estar entre 2 y 10 - ' + valorNumerico) } } – David Ortega Apr 28 '17 at 15:39
  • @David Ortega Update your question, do not add a comment. – Jeroen Heier Apr 28 '17 at 15:52

3 Answers3

0

From what I understand here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode

String.fromCharCode() will convert a number (or a series of numbers) into their corresponding sequence of unicode values; so whatever argument you pass to fromCharCode() shouldn't actually return a string that you can parse into an integer. In other words, when given a number, fromCharCode() will return something that isn't a numeric value.

I would do as others suggest and console.log(tecla) and console.log(e) to see what values you're really dealing with.

If you really are listening to a keyup or keydown event, then String.fromCharCode(e.keyCode) should be parseable by parseInt(). For example, if e.keycode returned the value 0x31, you should be able to to parse that into a string with fromCharCode() and into an integer with parseInt().

However, logging will always be your best bet when you're not quite sure what data you're dealing with, or if things just seem strange.

Bearlock
  • 1
  • 2
  • in other words i cant use this function for my porpouses.? So how can i implemented a function to validate the range input the user in a text box. thanks – David Ortega Apr 28 '17 at 15:45
  • You can totally use the function for your purposes, it just depends upon if you're passing it valid data. `String.fromCharCode(0x31)` will return "1" and you can `parseInt(1)` to get the integer value of 1. You need to verify and validate that you're actually passing in a valid keycode that corresponds to a number to `String.fromCharCode()` – Bearlock Apr 28 '17 at 15:59
0

parseInt() doesn't always assume base 10. That could be part of it.

But I really suspect here that tecla and te do not have the values you think they do. Try putting a console.log() statement right above your parseInt() call, so you can see what's really going to that function. It might surprise you.

You especially should read through this related question:

JavaScript KeyCode vs CharCode

Which tells you that this line:

tecla = (document.all) ? e.keyCode : e.which; 

Should perhaps look more like this:

tecla  = (typeof e.which == "number") ? e.which : e.keyCode

But even that still might not produce what you want for certain older browsers and non-printable characters, or if you're using something other than the keypress even, like keyup or keydown.

Community
  • 1
  • 1
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
0

The problem seems to be in these lines:

1) te = String.fromCharCode(tecla); // 5
2) var valorNumerico = Number(te);

Lookingat your code line1 seems to suggest that tecla is a number, also it should be as fromCharCode() function expects a number and returns a character; So let's say tecla is 65, so line1 would return 'A' that would be assigned to te variable.

Now in line2 you are trying to convert the character into a number, since (for e.g. A) it is not convertible to a number, you get NaN.

Hope it helps!

Pankaj Shukla
  • 2,657
  • 2
  • 11
  • 18