DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Javascript

02.10.2006
| 2813 views |
  • submit to reddit
        // description of your code here

<SCRIPT LANGUAGE="JavaScript1.1">
<!-- hide from non-scriptable browsers

// JavaScript sees numbers with leading zeros as octal values, so strip zeros
function stripZeros(inputStr) {
	var result = inputStr
	while (result.substring(0,1) == "0") {
		result = result.substring(1,result.length)
	}
	return result
}

// general purpose function to see if an input value has been entered at all
function isEmpty(inputStr) {
	if (inputStr == "" || inputStr == null) {
		return true
	}
	return false
}

// general purpose function to see if a suspected numeric input
// is a positive integer
function isNumber(inputStr) {
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.substring(i, i + 1)
		if (oneChar < "0" || oneChar > "9") {
			return false
		}
	}
	return true
}

// function to determine if value is in acceptable range for this application
function inRange(inputStr) {
	num = parseInt(inputStr)
	if ((num < 0) || (num > 255)) {
		return false
	}
	return true
}

function isValid(inputStr) {
	if (isEmpty(inputStr)) {
		alert("Please enter a number")
		return false
	} else {
		if (!isNumber(inputStr)) {
			alert("Please enter numbers only")
			return false
		} else {
			if (!inRange(inputStr)) {
				alert("Valid range is 0 - 255")
				return false
			}
		}
	}
	return true
}

function allDone(form) {
  var inputStr1 = form.entry1.value
  var inputStr2 = form.entry2.value
  var inputStr3 = form.entry3.value
  var inputStr4 = form.entry4.value

  var doneVal
  var ok2Cont   = true

	if ( isEmpty(inputStr1) || isEmpty(inputStr2) ||
	   isEmpty(inputStr3) || isEmpty(inputStr4) ) {
     ok2Cont = false ;
     alert("All fields need to be filled in")	;
	}

  if (ok2Cont) {
     form.octet1.value = toBin(inputStr1)
     form.octet2.value = toBin(inputStr2)
     form.octet3.value = toBin(inputStr3)
     form.octet4.value = toBin(inputStr4)

     // Concatenate binary characters
     doneVal = toBin(inputStr1) + toBin(inputStr2) + toBin(inputStr3) + toBin(inputStr4) ;

     form.all4.value = doneVal

     // strip any leading zero
     doneVal = stripZeros(doneVal) ;

     // convert binary to decimal
     form.result.value = "HTTP://" + parseInt(doneVal, 2) ;
  } else {
     form.octet1.value = ""
     form.octet2.value = ""
     form.octet3.value = ""
     form.octet4.value = ""
     form.result.value = ""
  }
}


// Decimal to binary, returns an eight character string
function toBin(inVal) {
   base = 2 ;
   num = parseInt(inVal);
   binNum = num.toString(base);
   // pad leading spaces with "0"
   binNum = padTextPrefix(binNum, "0", 8) ;

   return binNum
}

// equiv to padl()
function padTextPrefix (InString, PadChar, DefLength)  {
   if (InString.length >= DefLength)
      return (InString);
   OutString = InString
   for (Count = InString.length; Count < DefLength; Count++)  {
      OutString = PadChar + OutString;
   }
   return (OutString);
}

// end hiding -->
</SCRIPT>