Validate IP Address in javacript or jquery

by 3:45 AM 0 comments

Hi friends here is the best way to check that the input IPAddress is valid or not..!

so use this method to check it out..!
Write this method in the Script area and follow the steps..!
function verifyIP(IPvalue) {
    errorString = "";
    theName = "IPaddress";

    var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
    var ipArray = IPvalue.match(ipPattern);

    if (IPvalue == "0.0.0.0")
        errorString = errorString + theName + ': ' + IPvalue + ' is a special IP address and cannot be used here.';
    else if (IPvalue == "255.255.255.255")
        errorString = errorString + theName + ': ' + IPvalue + ' is a special IP address and cannot be used here.';
    if (ipArray == null)
        errorString = errorString + theName + ': ' + IPvalue + ' is not a valid IP address.';
    else {
        for (i = 0; i < 5; i++) {
            thisSegment = ipArray[i];
            if (thisSegment > 255) {
                errorString = errorString + theName + ': ' + IPvalue + ' is not a valid IP address.';
                i = 4;
            }
            if ((i == 0) && (thisSegment > 255)) {
                errorString = errorString + theName + ': ' + IPvalue + ' is a special IP address and cannot be used here.';
                i = 4;
            }
        }
    }
    extensionLength = 3;
    if (errorString == "") {
        //jAlert("That is a valid IP address.");
        //jAlert('That is a valid IP address.', 'VBrick');
        return true
    }
    else {
        jAlert(errorString, 'VBrick');
        return false
    }
}

-> isValidIp = verifyIP(ipAddress)
-> now check whether the ipaddress is valid or not using the isValidIP..
if the isValidIp == true then the address is correct otherwise its false..!



ravi tuwar

Developer

Cras justo odio, dapibus ac facilisis in, egestas eget quam. Curabitur blandit tempus porttitor. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.

0 comments:

Post a Comment