Sindre Moldeklev Sindre Moldeklev -4 years ago 175
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
statement won't be executed. I simply want to check that the user entered a positive integer.

Answer Source

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

if (parseInt(form.width.value) < 0) {
    .attr('placeholder', 'Please input the desired width of the table.')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download