Validate input - IP adress


#1

Hi, in my app, i need to validate input from user, but i need to check, if the input is correct ip adress. How can i do that, in a most simple way?


#2

See: https://angular.io/api/forms/PatternValidator


#3
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)  
}

Apart from pattern validator in angular js you can also use this function for tracking that passed input value is valid IP or not.