-9

Possible Duplicate:
Validate IP address is not 0.0.0.0 or multicast address
javascript regular expression to check for IP addresses

I need to perform IP Address validation that should accept 0-255 number range and dots.

I am trying with this code:

<script type='text/javascript'>
   function checkValid(eleValue) {

      if(!eleValue.match(/^[0-9]+\.?[0-9]*$/))
      {
         alert('Please enter Only Number or Dot');
      } 
   }
</script>

<input type="text" onkeyup='javascript:checkValid(this.value);' />

But its not working for me..

Community
  • 1
  • 1
Disha Goyal
  • 624
  • 12
  • 22

1 Answers1

3

you can use the following regex

/^(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]?)$/i

e.g.

/^(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]?)$/i.test('111.170.0.1') === true
amd
  • 20,637
  • 6
  • 49
  • 67