JavaScript Program to Check IP Address Format Using Regular Expression

Write a JavaScript function to check whether a given value is valid IP value or not.



Source Code
<html>
<head>
    <title>Check for valid IP address</title>
    <script>
    function checkIP()
    {
        var re = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
        var ip = myform.ip.value

        if(re.test(ip))
            alert("You have entered a Valid IP address")
        else
            alert("You have entered an Invalid IP address")
        }
    </script>
</head>
<body>
<form name="myform">
    Enter IP address <input type="text" id="ip"/>
    <input type="button" value="Submit" onclick="checkIP()"/>
</form>
<body>
</html>
Output