lundi 10 août 2015

Special ipPattern on JavaScript

I used this pattern to check if a field's form is a IP address or not:

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 < 4; 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;
      }
      if (thisSegment.toString() == "*")
        errorString = "";
      }
    }
    extensionLength = 3;
    if (errorString == "")
      alert ("That is a valid IP address.");
    else
      alert (errorString);
}

But I need to check if the value of the field have an asterisk '*' or a range '0-255'.

Like for example:

192.168.1.1 --> It will be OK
192.168.*.* --> It will be OK
192.168.2-3.0-128 --> It will be OK
192.168.2-3.* --> It will be OK

Any ideas? Thank you so much!

Aucun commentaire:

Enregistrer un commentaire