4

I'm trying to create a HTML5 pattern for input text with and IP address and mask together, like this:

10.120.15.30/28 or 172.181.30.0/24

I found one html5 pattern at http://html5pattern.com/Miscs but is only for IPv4 without mask

((^|\.)((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]?\d))){4}$

I tried add before

    (\/).((([0-2])|(0-9))|(3[1-2])) 

but is not working. Any ideas?

sampathsris
  • 21,564
  • 12
  • 71
  • 98
Alex Deiwor
  • 117
  • 3
  • 9
  • As webpage to test the pattern there is http://html5pattern.com/Make_Your_Own, is where i'm testing the patterns – Alex Deiwor Sep 30 '14 at 11:25

1 Answers1

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

Demo.

I simply added /(?:\d|[12]\d|3[01])to the pattern you provided:

/          // match a slash
(?:        // then match either one of
    \d     // a single digit
|
    [12]\d // any number from 10 to 29
|
    3[01]  // 30 or 31
)
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
  • Perfect! I was only using html5patterns, but with regex that works like a charm. Thank you! :) – Alex Deiwor Sep 30 '14 at 11:49
  • 1
    Aren't the prefixes only 24-31 as in http://en.wikipedia.org/wiki/Subnetwork#Subnet_and_host_counts, i.e. `(/(2[4-9]|3[01]))?` – Oleg Sep 30 '14 at 11:52
  • 1
    I think the first sollution was the correct, as I could remark a single host as xx.xx.xx.xx/32 – Alex Deiwor Sep 30 '14 at 14:50