简体   繁体   中英

Try to validate IP Address with javascript

I'm a beginner to javascript. Now, I'm trying to make a form to post back to server. There are some "input" that contains ip address which should be validate before submitting. Now I have done a javascript function which work well. But now I'm trying to add this function into jquery selection. Just confuse how to do it.

This is my validate javascript code.

function ValidateIPaddress(Ipfield)  
 {  
    IpAddr=Ipfield.value;
    var ipformat = /^(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]?)$/;  

    if(!IpAddr.match(ipformat))  
        return true;  
    else
        return false;

 }

and this is now how I implement for this validation.

<input type= "text" name= "LocalIP" style= "margin-right:10px " value="192.168.1.193" class="ip" onfocusout="ValidateIPaddress(document.getElementById('LocalIp'))" id="LocalIp" >            Remote VIP Address :             
                <input type= "text" name= "RemoteVIPAddr" style= "margin-right:10px" value="234.5.6.7" class="ip"  onfocusout="ValidateIPaddress(document.getElementById('RemoteIp'))" id="RemoteIp" >
                Remote VIP Port :             
                <input type= "text" name= "RemoteVIPPort" style= "margin-right:10px" value="5004" class="ip"  onfocusout="ValidatePort(document.getElementById('RemoteVIPPort'))" id="RemoteVIPPort">

Now I want to use jquery selection to always check if there are some invalid input. Which is something like this but with my own design function.

$("input.ip:visible").filter(function() { return this.ValidateIPaddress === true }).addClass("invalid");

Anyone has idea bout it?

You're not calling ValidateIPAddress in your filter function, you're just testing whether the DOM element has a non-empty property named ValidateIPAddress . It should be:

$("input.ip:visible").filter(function() {
    return ValidateIPAddress(this);
}).addClass("invalid");

Try this:

isIP(ip) {
  if (typeof(ip) !== 'string')
    return false;
  if (!ip.match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)) {
    return false;
  }
  return ip.split('.').filter(octect => octect >= 0 && octect <= 255).length === 4;
}

Original: https://stackoverflow.com/a/50612630/3261332

And if one needs to accept also CIDR format IP/{0-32} please update the 2 lines as below:

if (!ip.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(\/([0-9]|[12][0-9]|3[0-2]))?$/)) {
return ip.split('/')[0].split('.').filter(octet => octet >= 0 && octet <= 255).length === 4;

See if this help. This is valid fo IP4 only. 0.0.0.0 - Invalid Any ip with CIDR is invalid

function validateIP(ip) {
    is_valid = false;
    ip = ip.replace(/\s+/, "");

    if(ip.indexOf('/')!=-1){
        alert("IP not valid");
        return false
    }
    
    try {
        var ipb = ip.split('.');
        if (ipb.length == 4) {
            for (i = 0; i < ipb.length; i++) {
                b = parseInt(ipb[i]);    
                if (b >= 0 && b <= 255) {
                    is_valid = true;
                } else {
                    is_valid = false;
                    break;
                }
            }
        }
    } catch (exception) {
        alert("IP is not valid")
        return false;
    }
    if (!is_valid) {
        alert("IP is not valid")
        return false;
    }
    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM