widsnet.com
22Jul/140

Javascript to check IP syntax

My first javascript. Colors the inputbox green if the syntax is correct. Simple check to test if the IP entered is "valid". Meaning an IP between 1.0.0.0 and 255.255.255.255.
Regexp pattern can easily be modified to check for some other known syntax. Computername or such.

<script type="text/javascript">
function ipCheck(formID) {
	var patt = /^([1-9]|[1-9][0-9]|[1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])([.]([0-9]|[1-9][0-9]|[1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])){3}$/i;
	var form = document.getElementById(formID);
	if (patt.test(form.value)) {
		/* green if correct */
		form.style['backgroundColor'] = "#00FF00";
		/* form.value = form.value.toUpperCase(); 
		I used this for another function to check for the computername syntax 
		and force it to be uppercase when entered correct. */
	} else if (form.value == "") {
		/* Remove color if blank value */
		form.style['backgroundColor'] = "";
	} else {
		/* yellow otherwise */
		form.style['backgroundColor'] = "#FFFF00";
	}
}
</script>
<input type="text" name="Subnet" id="Subnet" Value="<%=sSubnet%>" Maxlength="15" onmouseout="ipCheck('Subnet')" onkeyup="ipCheck('Subnet')">
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.