-1

Please help me find what is wrong with my code

Number or Letter

<input type="text" id="text">
<button onclick="Check()" type="button" name="button" id="btnCheck">Check</button>

<script type="text/javascript">
  function Check(){
    var x = parseFloat(document.getElementById("text").value);

    if (isNan(parseFloat(x))) {
      alert("This is a letter");
    }else {
      alert("This is a number");
    }
  }
</script>
Kasturi R
  • 21
  • 6
  • `isNaN(parseFloat(x))` - typo - `isNaN` not `isNan` – Arun P Johny Feb 23 '16 at 06:35
  • Also note that `parseFloat` should not be applied twice..refer https://jsfiddle.net/rayon_1990/oqo0rm3w/1/ – Rayon Feb 23 '16 at 06:38
  • You're happy with `"21foobar"` then? – RobG Feb 23 '16 at 06:45
  • Probably a duplicate of [*Is there a (built-in) way in JavaScript to check if a string is a valid number?*](http://stackoverflow.com/questions/175739/is-there-a-built-in-way-in-javascript-to-check-if-a-string-is-a-valid-number/175787#175787), but the OP may wish to check single characters, where a regular expression test would be more suitable I think. – RobG Feb 23 '16 at 06:57

2 Answers2

1

This is a common error.

The correct syntax is isNaN not isNan

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN

For other people that have the same question, here is one way to debug this.

Open up your JavaScript console and view the error messages. If you see something like

isNan is not defined

That means there is a syntax error.

There are two ways the code can be improved

  1. By removing the second reference to parseFloat. You already converted to a number.
  2. We can change the if-else statement to ternary.

<input type="text" id="text">
<button onclick="Check()" type="button" name="button" id="btnCheck">Check</button>

<script type="text/javascript">
  function Check(){
    var x = parseFloat(document.getElementById("text").value);
    alert(isNaN(x) ? "This is a letter" : "This is a number");
  }
</script>
Richard Hamilton
  • 24,108
  • 10
  • 56
  • 82
  • Except that the logic is seriously flawed. `parseFloat('21foobar')` returns `21`, so an *isNaN* test returns *false*. Far better to use a regular expression. ;-) – RobG Feb 23 '16 at 06:47
-1

Use jquery $.isNumeric() The $.isNumeric() method checks whether its argument represents a numeric value. If so, it returns true. Otherwise it returns false. The argument can be of any type.

$.isNumeric( "-10" );     // true
$.isNumeric( 16 );        // true
$.isNumeric( 0xFF );      // true
$.isNumeric( "0xFF" );    // true
$.isNumeric( "8e5" );     // true (exponential notation string)
$.isNumeric( 3.1415 );    // true
$.isNumeric( +10 );       // true
$.isNumeric( 0144 );      // true (octal integer literal)
$.isNumeric( "" );        // false
$.isNumeric({});          // false (empty object)
$.isNumeric( NaN );       // false
$.isNumeric( null );      // false
$.isNumeric( true );      // false
$.isNumeric( Infinity );  // false
$.isNumeric( undefined ); // false

Using JavaScript :-

function alphanumeric(inputtxt)  
{   
var letters = /^[0-9a-zA-Z]+$/;  
if(inputtxt.value.match(letters))  
{  
alert('Your registration number have accepted : you can try another');  
document.form1.text1.focus();  
return true;  
}  
else  
{  
alert('Please input alphanumeric characters only');  
return false;  
}  
}  
Salah Nour ElDin
  • 500
  • 2
  • 13
  • [tag: javascript]: *"Unless another tag for a framework/library is also included, a pure JavaScript answer is expected."* – Felix Kling Feb 23 '16 at 06:48
  • Also probably not what the OP wants: "0xFF" is unlikely to be considered a number by most humans. – RobG Feb 23 '16 at 06:59
  • Seems like you copied&pasted code from [this question](http://stackoverflow.com/q/11321963/218196) without giving credit. Please don't do that. If you find that this is a duplicate, vote/flag to close it as such or if it is related, link to it. – Felix Kling Feb 23 '16 at 15:56
  • where is copy and past part that i taken – Salah Nour ElDin Feb 24 '16 at 06:38