Valid IPv4 address
A free easy coding practice problem from networking. Solve it in JavaScript, TypeScript or Python and check your solution against fresh hidden tests — edge cases and random inputs — as often as you like.
- Difficulty
- Easy
- Type
- Function
- Topic
- Networking
- Languages
- JavaScript, TypeScript or Python
Problem
Return true if s is a valid dotted-decimal IPv4 address: exactly four parts separated by single dots, each part made only of digits 0–9, with a value from 0 to 255 and NO leading zeros ("0" is fine, "01" is not). Anything else (spaces, signs, empty parts, extra dots) → false. s has at most 30 characters.
Function signature
// JavaScript
function isValidIpv4(s)
// TypeScript
function isValidIpv4(s: any): any
// Python
def is_valid_ipv4(s):Examples
isValidIpv4("192.168.1.1")→trueisValidIpv4("256.1.1.1")→falseisValidIpv4("10.01.0.1")→false
How checking works
Run your code against the examples above, then submit a check: it runs on the server against hidden tests built fresh each time, including edge cases and random inputs. Practice is untimed and never affects a verified score; when you are ready, take a graded coding challenge to earn a badge.