Sindre Moldeklev Sindre Moldeklev - 3 months ago 11
Javascript Question

JavaScript parseInt-error

I have an HTML form with a few input-fields, and I want to check that the users enter a number into a text field, instead of a string. I tried this:

if(parseInt(form.width.value) < 0) {
$("#width").attr('placeholder', 'Please input the desired width of the table.');
}


I have tried with a negative number, a positive number, text, but nothing happends. But whatever I try, the code inside the
if
statement won't be executed. I simply want to check that the user entered a positive integer.

Answer

You need to clear the inputted value, otherwise the placeholder won't be displayed.

if (parseInt(form.width.value) < 0) {
  $('#width')
    .attr('placeholder', 'Please input the desired width of the table.')
    .val('');
}
Comments