OG Frag
10-09-2007, 11:21 PM
I need to devise a way for the script to display "Invalid binary number" whenever the input is not a valid 1's and 0's binary number, but I'm having trouble with it. This is what I have so far (didn't include variables)
for (i = 0; i < input.length; i++){
if(input.charAt(i) == '1' || input.charAt(i) == '0'){
document.BinaryForm.OutputText.value = ""
validinput = true;
}else if(input.charAt(i) != '1' && input.charAt(i) != 0){
document.BinaryForm.OutputText.value = "Invalid binary number"
validinput = false;}
It disregards the first digit when I run the script, calculating the decimal value for the rest of it (for example, if I put in 50001, it comes up as two rather than the correct value of 1)
Any help is appreciated.
for (i = 0; i < input.length; i++){
if(input.charAt(i) == '1' || input.charAt(i) == '0'){
document.BinaryForm.OutputText.value = ""
validinput = true;
}else if(input.charAt(i) != '1' && input.charAt(i) != 0){
document.BinaryForm.OutputText.value = "Invalid binary number"
validinput = false;}
It disregards the first digit when I run the script, calculating the decimal value for the rest of it (for example, if I put in 50001, it comes up as two rather than the correct value of 1)
Any help is appreciated.