简体   繁体   中英

How to validate if IP address is correct?

I am creating snmp session everything is working as expected , i will be getting dynamic value of host to create session, I just want to put if condition to check Ip address is correct or not , is there way to validate IP address in javascript ?

main.js

var host = "135.01.01.01";
var sessionOptions = {
    port: 161,
    retries: 1,
    timeout: 5000,
    transport: "udp4",
    trapPort: msg.event.body.trapPort,
    version: snmpVersion
};
//Create snmp Session
var session = snmp.createSession(host, "public", sessionOptions);

try {
    if (!host) {
        console.log("Could not find host");
    } else {
        session.trap(trapOid, varbinds, options, function(error) {
            if (error)
                console.log(error);
            else
                console.log('SNMP successfully delivered');
        });
    }
} catch (e) {
    console.log("SNMP processing error: " + e);
}

Adapted from here :

function ValidateIPaddress(ipaddress)   
{  
 if (/^(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]?)$/.test(host))  
  {  
    return (true)  
  }  
return (false)  
}  

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