0

I'm trying to use jQuery and AJAX to validate that users entered a number in a particular field and that they didn't leave it blank and I'm a little confused as to why I can seem to do one, but not the other.

I'm doing this in a jQuery change() function so any time they change the value in that field, it updates it in the database without refreshing the whole page and it works fine until I try to use isNull() to validate.

I'm saving their input to a variable called UserInput and first checking to make sure it's a number with this:

if (!isNaN(UserInput))

which works perfectly. I'm also trying to check and make sure it isn't empty by using this:

if (isNull(UserInput))

Intellisense completes isNull() for me just like it did for isNaN() and all appears well in Visual Studio, it compiles without error. I've also tried isNullOrUndefined() here with a similar result, intellisense completes it for me and all seems well. Right up until I change the value in the field, at which point it promptly gives me this error:

JavaScript runtime error: 'isNull' is undefined.

I'm not sure why it's undefined (especially since intellisense is completing it for me) or how to go about defining it.

I also tried this because it seemed like it covered all the bases, not just isNull():

https://stackoverflow.com/a/5515349/8767826

and I put an alert() inside the if and I didn't get an error, but my alert didn't fire either.

The end goal is to get it to change to a zero on the client side if they leave do leave it blank.

Anyway I'm kind of stumped and I appreciate any help anyone can offer.

Thanks

10
  • you can easily check if($('#id_of_your_input').val().length == 0) {alert('required'); return; } Commented Oct 24, 2018 at 20:30
  • 4
    "I'm not sure why it's undefined" isNull is not a built-in JavaScript function. Commented Oct 24, 2018 at 20:30
  • 4
    Unless you've defined the isNull() and isNullOrUndefined() functions in JS, then your Intellisense is lying to you. They are not standard JS functions. Assuming you're referring to Intellisense in Visual Studio, then I would ignore everything it tells you for JS. It is notoriously unreliable. Commented Oct 24, 2018 at 20:31
  • 1
    Also, and all appears well in Visual Studio, it compiles without error.. JS is not a compiled language. JS is considered an interpreted language, that is, JS code is not looked at by the browser until that line is ran. Commented Oct 24, 2018 at 20:35
  • 1
    For reference, check out String.IsNullOrEmpty in JavaScript. Commented Oct 24, 2018 at 20:54

1 Answer 1

1

There's no need for an isNull function; you can check

if (UserInput === null)

isNaN exists because NaN, unlike every other value in JavaScript, is not equal to itself.

But null doesn't mean the field is blank! If the field is blank, its value will be the empty string. Check for that instead:

if (UserInput === '')
Sign up to request clarification or add additional context in comments.

Comments

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.