1

I'm really new to javascript and I'm trying to prevent a textbox from accepting letters and empty values. This is what I have:

function checkInp()
{
var x=document.forms["newPerson"]["ThisBox"].value;
    if (isNaN(x) || is_null(x)) 
    {
        alert("This box can only contain numeric values");
        return false;
    }
}

Here is my form tag:

<form name="newPerson" action="newPerson2.php" method="POST" onsubmit="return checkInp();">

This DOES prevent letters from passing through, but it DOES NOT prevent empty values from going through.

1
  • Remember undefined is not === null Commented Nov 19, 2015 at 2:33

1 Answer 1

3

Try to check like this

if(!x || isNaN(x)){
  alert("This box can only contain numeric values");
  return false;
}

In javascript "",null,undefined,NaN,0 all consider false.

Sign up to request clarification or add additional context in comments.

1 Comment

I recommend this approach as well. This is called falsey meaning "Something which evaluates to FALSE." Read more: james.padolsey.com/javascript/truthy-falsey

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.