taesik

isValidIP 본문

functions and trick/typescript_javascript

isValidIP

taesikk 2022. 1. 26. 09:38

Number( sth )

can check it is number or string with only number. 

if sth contains other, it would return Nan. 

function isValidIP(str) {
return str  .split('.')
			.filter(v => v == Number(v).toString() && Number(v) >= 0 && Number(v) < 256)
            .length == 4;
}​
Examples of valid inputs:
1.2.3.4
123.45.67.89

 

1.2.3
1.2.3.4.5
123.456.78.90
123.045.067.089