regexhtmlip

HTML5 pattern for validating IP with mask


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?


Solution

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

    Demo.

    I simply added /(?:\d|[12]\d|3[012])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 32
    )