- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>Sample Code</title>
- <script type="text/javascript">
- function CheckIPAddress(MyIPAddress)
- {
- var CheckIP = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
- if(CheckIP.test(MyIPAddress))
- {
- alert("Your IP Address Is Valid.");
- }
- else
- {
- alert("Your IP Address Is Not Valid.");
- }
- }
- </script>
- </head>
- <body bgcolor=#00ff00 alink=blue vlink=blue>
- <FORM name="windowEvent">
- IP Address : <input type="text" name="txtipaddress" />
- <input type="button" value="Check" name="btnIPAddress" onClick="CheckIPAddress(txtipaddress.value)" />
- </FORM>
- </body>
- </html>
Javascript-Check IP Address using Regex
This code shows you that how you can check the user's given value is Valid IP Address or not using javascript on button click event.


David ScherfgenPosted Nov 30, 2012, 9:12 AM EST
Hi, this regular expression doesn't make sure that the numbers are between 0 and 255. It would accept an IP address like 999.888.777.666, which is clearly wrong. See my article here, it contains a regex that does check for this: http://regex-for.com/ip-addresses/