StacyMidget StacyMidget - 4 months ago 35
jQuery Question

If else Jquery for IP number

I'm trying to test to see if my IP number matches with the one specified. I'm new to Javascript, and not sue what's wrong with my code.



$(document).ready(function() {
$.getJSON("http://jsonip.com/?callback=?", function(data) {
console.log(data);
var userIP = (data.ip);
var myIP = 172.16.254.1;

if (userIP == myIP) {
$("body").html("<p>IP Login Success!</p>");
} else {
$("body").html("<p>Invalid IP!</p>");
}
});
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>





How can I make it see if the IP matches the users?

Answer

Try:

var myIP = "172.16.254.1";

You are assigning a literal string to myIP.