Advertisement
CSenshi

tmp

Apr 28th, 2020
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   // regex to check that each octet is valid
  2.     var er = /^[0-9]+$/;
  3.     // ipv4 octets are delimited by dot
  4.     octets = val.split('.');
  5.     // check 1: ipv4 address should contains 4 octets
  6.     if (octets.length != 4)
  7.       return false;
  8.  
  9.     for (let i = 0; i < octets.length; i++) {
  10.       const element = octets[i];
  11.       // check 2: each octet should be integer bigger than 0
  12.       if (!er.test(element))
  13.         return false;
  14.  
  15.       // check 3: each octet value should be less than 256
  16.       var octetValue = parseInt(element);
  17.       if (octetValue >= 256)
  18.         return false;
  19.     }
  20.  
  21.     // if all checks passed, we know it's valid IPv4 address!
  22.     return true;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement