Select to view content in your preferred language

Test for NaN or undefined Coordinates?

3913
4
Jump to solution
11-19-2015 10:42 AM
LauraMiles1
Frequent Contributor

I have a function that grabs coordinates from a text input box, and zooms to that point using centerAndZoom. I want to detect if the user enters something invalid, so I can pop up an alert informing them of the valid format, and abort the zoom. Currently it zooms to NaN, NaN if something invalid is entered.

The point retrieved from the textbox is called zoomPoint. I've tried:

if (zoomPoint === "undefined")

if (zoomPoint === "NaN")

if (zoomPoint === null)

if ((zoomPoint.x === "undefined") || (zoomPoint.y === "undefined"))

if ((zoomPoint.x === "NaN") || (zoomPoint.y === "NaN"))

if ((zoomPoint.x === null) || (zoomPoint.y === null))

None of these work when something invalid is entered, and the map zooms to coordinates NaN, NaN. When I popup an alert box containing zoompoint.x or zoompoint.y, the value shows as undefined - why doesn't the test if ((zoomPoint.x === "undefined") || (zoomPoint.y === "undefined")) work then?

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Do not put quotes around undefined and null when doing a comparison like that. In one of my apps, I check whether a value is valid like this:

var value = registry.byId("textInput").value;
if (value !== "" && value !== null && value !== undefined) {
   //do something
} 

In the case of numeric value from a NumberTextBox, I also check if it's a number.

var value = registry.byId("numberInput").value;
if (value !== "" && value !== null && value !== undefined) {
    if (registry.byId("numberInput") instanceof NumberTextBox) {
        if (!isNaN(value)) {
            //do something
        }
    }
}

View solution in original post

0 Kudos
4 Replies
RobertScheitlin__GISP
MVP Emeritus

Laura,

   If the input is a text box then the value of an empty text box would be "" not NaN unless you are using a Dojo NumberTextBox. Check for an empty string instead.

0 Kudos
LauraMiles1
Frequent Contributor

I have also tried testing for zoomPoint.x or y === "", but this did not work. zoomPoint is actually a geometry point, not the text box contents - I'm trying to determine if the user entered valid coordinates, ie. in case the user puts an "N" before or after the longitude, or gets some other text characters in by accident, etc - I think Ken's answer is working for this.

0 Kudos
KenBuja
MVP Esteemed Contributor

Do not put quotes around undefined and null when doing a comparison like that. In one of my apps, I check whether a value is valid like this:

var value = registry.byId("textInput").value;
if (value !== "" && value !== null && value !== undefined) {
   //do something
} 

In the case of numeric value from a NumberTextBox, I also check if it's a number.

var value = registry.byId("numberInput").value;
if (value !== "" && value !== null && value !== undefined) {
    if (registry.byId("numberInput") instanceof NumberTextBox) {
        if (!isNaN(value)) {
            //do something
        }
    }
}
0 Kudos
LauraMiles1
Frequent Contributor

Yep, thanks Ken - for some reason NetBeans was showing an error when I first tried undefined without the quotations, but it's working now!

0 Kudos