0

I want to validate an ip address with custom format:

Format i expected : ip address - any value - anynumber

Format above have 4 parts:

  1. ip address (contain a valid ip address)
  2. any value (contain any value)
  3. any number (contain only 1 to 3 digits of number)
  4. connecting parts (contain space,dash,space ( - ))

Example : 213.39.59.78 - Public3 address.info - 24

function customFormat(val) {
  return /^(?=\d+\.\d+\.\d+\.\d+$)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.?){4}$/.test(val);
}
var testFormat = '192.68.35.35';
document.getElementById('regex').innerHTML += testFormat + ' ' + (customFormat(testFormat) ? 'Valid Format!' : 'Invalid Format!');
<ul id="regex"></ul>

Code above used regular expression from here, but just validate the ip address.

How to validate an ip address like format i expected?

Ras
  • 991
  • 3
  • 13
  • 24
  • You need to add something like ` - [^-]+- \d+` before the final *$* in the RegExp, but I think doing this with a regular expression results in very obfuscated code. – RobG Mar 26 '18 at 05:28

4 Answers4

1

Here's something you could try:

function customFormat(val) {
  return /^(?=\d+\.\d+\.\d+\.\d+)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.?){4} - ((?! -).)+ - \d{1,3}$/.test(val);
}
var testFormat = '213.39.59.78 - Public3 address.info - 24';
document.getElementById('regex').innerHTML += testFormat + ' ' + (customFormat(testFormat) ? 'Valid Format!' : 'Invalid Format!');
<ul id="regex"></ul>

Breakdown:

Regex:

/^(?=\d+\.\d+\.\d+\.\d+)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.?){4} - ((?! -).)+ - \d{1,3}$/

The regex has 5 parts that are pretty much the parts you listed:

  1. /^
    • Matches the beginning of string
  2. (?=\d+\.\d+\.\d+\.\d+)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.?){4}
    • ip address (contain a valid ip address)
  3. -
    • connecting parts (contain space,dash,space ( - ))
  4. ((?! -).)+
    • any value (contain any value)
    • important: this part uses a lookahead and matches one or more chars that are not the connecting part. In other words, it will match anything until it finds a -.
  5. -
    • connecting parts (contain space,dash,space ( - ))
  6. \d{1,3}
    • any number (contain only 1 to 3 digits of number)
  7. $/
    • End of string.
acdcjunior
  • 132,397
  • 37
  • 331
  • 304
0

enter image description hereTry this... It works for me

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(myForm.emailAddr.value))
  {
    return (true)
  }
  alert("You have entered an invalid IP address!")
  return (false)
}
Prakash Jethava
  • 200
  • 1
  • 7
0

By parsing the string and testing each part you'll get easier to follow code and the ability to return errors based on what went wrong if it fails the tests, e.g.:

function checkIP(s) {
  var t = s.split(' - ');

  // Test number of parts
  if (t.length != 3)
    return 'Error: format must be <IP address> - <string, no "-"> - <1 to 3 digits>: ' + s;

  // Test IP address
  if (!t[0].split('.').every(v => v >= 0 && v <= 255))
    return 'Error: IP address is invalid: ' + t[0];

  // Test trailing number
  if (!/^\d{1,3}$/.test(t[2])) 
    return 'Error: String must end with 1 to 3 digits: ' + t[2];

  // Must be valid
  return 'Valid string';
}

// Some tests
['213.39.59.78 - Public3 address.info - 222',
 '213.39.59.78 - Public3 address.info - 2222',
 '213.39.59.78 - Public3 address.info',
 '213.39.59.788 - Public3 address.info - 222'
].forEach(s => console.log(`Testing "${s}"\n ${checkIP(s)}`));

You might want to throw errors instead of just returning a string with an error message.

RobG
  • 142,382
  • 31
  • 172
  • 209
-1

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(ipaddress)) {
return (true)
}
alert("You have entered an invalid IP address!")
return (false)
}