You are here: irt.org | FAQ | JavaScript | Number | Q852 [ previous next ]
This is hard to do in anything other than JavaScript 1.2. The following code provides support for JavaScript 1.2+ by using regular expressions, and support for lesser versions of JavaScript with repetitive string validation:
<script language="JavaScript"><!--
var validChars = '.0123456789';
function validateIP(what) {
if (!what)
return false;
dots = 0;
for (var i = 0; i < what.length; i++) {
var chr = what.substring(i,i+1);
if (validChars.indexOf(chr) == -1)
return false;
if (chr == '.') {
dots++;
eval('dot' + dots + ' = ' + i);
}
}
if (dots != 3)
return false;
if (what.substring(0,1) == '.' || what.substring(what.length,what.length+1) == '.')
return false;
ip1 = what.substring(0,dot1);
if (!ip1 || ip1 >255)
return false;
ip2 = what.substring(dot1+1,dot2);
if (!ip2 || ip2 >255)
return false;
ip3 = what.substring(dot2+1,dot3);
if (!ip3 || ip3 >255)
return false;
ip4 = what.substring(dot3+1,what.length+1);
if (!ip4 || ip4 >255)
return false;
if (ip1 == 0 && ip2 == 0 && ip3 == 0 && ip4 == 0)
return false;
return true;
}
//--></script>
<script language="JavaScript1.2"><!--
function validateIP(what) {
if (what.search(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) != -1) {
var myArray = what.split(/\./);
if (myArray[0] > 255 || myArray[1] > 255 || myArray[2] > 255 || myArray[3] > 255)
return false;
if (myArray[0] == 0 && myArray[1] == 0 && myArray[2] == 0 && myArray[3] == 0)
return false;
return true;
}
else
return false;
}
//--></script>
<form>
<input name="input" type="text" value="18.128.93.86">
<input type="button" value="Validate" onClick="if (!validateIP(this.form.input.value)) alert('invalid'); else alert('valid')">
</form>The following test harness can be used to test that the above code works correctly:
<script language="JavaScript"><!--
function validateAll(what) {
for (var i=0; i<23; i++) {
if (validateIP(what['input' + i].value))
what['output' + i].value = 'valid';
else
what['output' + i].value = 'invalid';
}
}
//--></script>
<form>
<input name="input0" type="text" value="18.128.93.86"> <input name="output0" type="text" value=""><br>
<input name="input1" type="text" value="127.0.0.0"> <input name="output1" type="text" value=""><br>
<input name="input2" type="text" value="255.255.255.255"> <input name="output2" type="text" value=""><br>
<input name="input3" type="text" value="0.0.0.0"> <input name="output3" type="text" value=""><br>
<input name="input4" type="text" value="255.256.255.255"> <input name="output4" type="text" value=""><br>
<input name="input5" type="text" value="255.255.256.255"> <input name="output5" type="text" value=""><br>
<input name="input6" type="text" value="255.255.255.256"> <input name="output6" type="text" value=""><br>
<input name="input7" type="text" value="1234.255.255.255"> <input name="output7" type="text" value=""><br>
<input name="input8" type="text" value="255.1234.255.255"> <input name="output8" type="text" value=""><br>
<input name="input9" type="text" value="255.255.1234.255"> <input name="output9" type="text" value=""><br>
<input name="input10" type="text" value="255.255.255.1234"> <input name="output10" type="text" value=""><br>
<input name="input11" type="text" value=".255.255.255"> <input name="output11" type="text" value=""><br>
<input name="input12" type="text" value="255..255.255"> <input name="output12" type="text" value=""><br>
<input name="input13" type="text" value="255.255..255"> <input name="output13" type="text" value=""><br>
<input name="input14" type="text" value="255.255.255."> <input name="output14" type="text" value=""><br>
<input name="input15" type="text" value="255255255255"> <input name="output15" type="text" value=""><br>
<input name="input16" type="text" value="255,255,255,255"> <input name="output16" type="text" value=""><br>
<input name="input17" type="text" value=""> <input name="output17" type="text" value=""><br>
<input name="input18" type="text" value="...."> <input name="output18" type="text" value=""><br>
<input name="input19" type="text" value="a.b.c.d"> <input name="output19" type="text" value=""><br>
<input name="input20" type="text" value="255.255.255.255."> <input name="output20" type="text" value=""><br>
<input name="input21" type="text" value=".255.255.255.255"> <input name="output21" type="text" value=""><br>
<input name="input22" type="text" value="256.255.255.255"> <input name="output22" type="text" value=""><br>
<input type="button" value="Validate" onClick="validateAll(this.form)">
<input type="reset">
</form>