0

I need to verify myFunction to run at the time of onblur event. I tried below but didn't work.

HTML form:

<div class="header1">
    <input type="text" id="ename" onblur="myFunction('ename')" name="name"  placeholder="Enter Employee ID" required="" />
</div>

JavaScript: code

<script>
    function myFunction('id') {
        var x = document.getElementById('id');
        if (x === parseInt(x, 10))
            alert("data is integer")
        else
            alert("data is not an integer")               
        }
</script>
Tolga Evcimen
  • 7,112
  • 11
  • 58
  • 91
  • 1
    Possible duplicate of [Check whether variable is number or string in javascript](http://stackoverflow.com/questions/1303646/check-whether-variable-is-number-or-string-in-javascript) – Max Apr 27 '16 at 07:57
  • Use this simple trick for checking if it is an absolute integer x % 1 == 0 – Samyukta R. Apr 27 '16 at 07:58
  • you can also use the "pattern" attribute to allow only number, e.g. pattern="[0-9]+" – lipp Apr 27 '16 at 08:00
  • if the length of the employer id is fixed, you can even tell the pattern how many digits the number must have. – lipp Apr 27 '16 at 08:01

4 Answers4

0

Fastest (highest performance) integer check:

<div class="header1">
  <input type="text" id="ename" onblur="enteredInteger()" name="name" placeholder="Enter Employee ID" required="" />
</div>
<script>
  function enteredInteger() {
    var value = this.value;
    if (isNaN(value)) {
      return false;
    }
    var x = parseFloat(value);
    return (x | 0) === x;
  }
</script>
wilsonzlin
  • 2,154
  • 1
  • 12
  • 22
  • In what case does this particular function return true?I tried isInt(21), isInt("21"), both returned false. – Satej S Apr 27 '16 at 07:59
  • You should modify the approach unless there is something horribly wrong in that.. Also highlight what went wrong in the code OP has provided... This question is not regarding _performance_! – Rayon Apr 27 '16 at 08:01
  • You're both wrong, it works fine... isInt("21") === true – wilsonzlin Apr 27 '16 at 08:01
0

You have a little syntax mistake in your code. Try this:

// Javascript Code:

function myFunction(id)
{
  var x = document.getElementById(id);
  if (x.value == parseInt(x.value, 10))
    alert("data is integer");
  else
    alert("data is not an integer");
}
<!-- HTML Code: -->

<div class="header1">
            <input type="text" id="ename" onblur="myFunction('ename')" name="name"  placeholder="Enter Employee ID" required="" />
     </div>
      
Sharad Saxena
  • 319
  • 4
  • 16
0

Here we go:

<div class="header1">
    <input type="text" id="ename" onblur="myFunction()" name="name"  placeholder="Enter Employee ID" required="" />
</div>

<script>
function myFunction(){
  //use the === operator as below

    if (data === parseInt(data, 10))
        alert("data is integer");
    else
        alert("data is not an integer");
}
</script>

Hope it helps;)

Husni Salax
  • 1,968
  • 1
  • 19
  • 29
0

I would use a simple conversation to integer and test the original value with a string representation of the parsed value.

The task lakcks some coherent use offormular elements, which here are corrected.

function myFunction(id) {
    var x = document.getElementById(id).value;
    if (x === parseInt(x, 10).toString()) {
        alert("data is integer");
    } else {
        alert("data is not an integer");
    }
}
<input type="text" id="ename" onchange="myFunction('ename')" name="name"  placeholder="Enter Employee ID" required="" />
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392