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

Jay
  • 258
  • 1
  • 5
  • 14
  • you can easily check if($('#id_of_your_input').val().length == 0) {alert('required'); return; } – Senad Meškin Oct 24 '18 at 20:30
  • 4
    *"I'm not sure why it's undefined"* `isNull` is not a built-in JavaScript function. – Felix Kling Oct 24 '18 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. – Rory McCrossan Oct 24 '18 at 20:31
  • *"The end goal is to get it to change to a zero on the client side if they leave do leave it blank."* `UserInput = UserInput === '' ? 0 : Number(UserInput)` (still needs an `isNaN` check). – Felix Kling Oct 24 '18 at 20:33
  • 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. – Rich Oct 24 '18 at 20:35
  • @Rich: That's not quite true. The browser certainly *parses* the code before it is executed. I.e. if I have `function foo() { ( }` then this will immediately result in a syntax error, even though the function is never executed. – Felix Kling Oct 24 '18 at 20:50
  • @Rich javascript is not a **pre** compiled language. It is indeed compiled though. – Taplar Oct 24 '18 at 20:51
  • 1
    For reference, check out [String.IsNullOrEmpty in JavaScript](https://codereview.stackexchange.com/questions/5572/string-isnullorempty-in-javascript). – showdev Oct 24 '18 at 20:54
  • What other libraries are you using in your project? Do you also have underscore there? – xxxmatko Oct 24 '18 at 21:07
  • @FelixKling No need for the ternary, `Number('') === 0` – AuxTaco Oct 24 '18 at 23:39

1 Answers1

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 === '')
AuxTaco
  • 4,257
  • 1
  • 8
  • 24