Saturday, January 17, 2009

Validate user IP address using javascript

1. Read user IP address.
var ip = '<!--#echo var="REMOTE_ADDR"-->'

2. Enter list of allowed ips
var allowed_ips = ["11.11.11.11","22.22.22.22"]

3. Create a regular expression string.
var handleips = allowed_ips.join("|")
handleips = new RegExp(handleips, "i")

4. Check condition whether user IP address is in allowed list
if (ip.search(handleips)!=-1)
    alert("done")
else
    alert("Unauthorized access");

No comments: