...

View Full Version : Help validating a text box.



supacook2k
12-04-2006, 12:42 PM
to add a value to my text box using buttons i've created i have created this function

function newdig(val) {

document.type.display.value+=val;

}


it works fine but now i need to validate the text field so that only a numerical value can be the last digit input into the display when the submit button is clicked.

any help would be great

thanks

bhakti_thakkar
12-04-2006, 12:52 PM
i did not get this:
only a numerical value can be the last digit input into the display when the submit button is clicked.

bhakti_thakkar
12-04-2006, 12:56 PM
i did not get this:
only a numerical value can be the last digit input into the display when the submit button is clicked.
Do you mean the last character has to be a number ?

brandonH
12-04-2006, 01:18 PM
if you are wanting to check only the last character of he input do as so:





<form name=type id=type onsubmit="return validateIt();">
<input type=text name=display id=display>
<input type=submit>
</form>

<script type=text/javascript>
function validateIt(){
var input=document.type.display.value;
var length=input.length-1;
if(isNaN(input.charAt(length))){return false;}
else{return true;}
}
</script>

supacook2k
12-05-2006, 08:23 PM
thanks for the help, i've sort of got it working but there's another problem. when my calculation is in the display and i click equals if the last character is not a number then it alerts the user the calculation must end with a digit but when the calculation in the display is e.g. 9+9 its still alerts saying calculation must end in a digit and performs the calculation after clicking ok, basically i just want it to alert when the last character is not a number.

here's the code i've got for it. (when calling the functions i've got onClick="validateIt();equals();"



function equals() {

document.calculator.display.value=eval(document.calculator.display.value);

}

function validateIt() {

var input=document.calculator.display.value;
var length=input.length-1;

if(isNaN(input.charAt(length)));

alert("your calculation must end in a digit")

}

supacook2k
12-06-2006, 12:25 AM
i've updated my last post. sort of got it working but got a problem. listed problem in last post.

brandonH
12-06-2006, 02:10 AM
just a few syntax errors...... the below should do it. and i'm assuming that if the last characters isnt a number, you dont want it to calculate. so do not put the call to the calculate() function after the call to the validatit() function, instead do as below. if the character is not a number alert, if it is a number call to the calculate function.


<script type=text/javascript>

function equals() {
document.calculator.display.value=eval(document.calculator.display.value);
}

function validateIt() {
var input=document.calculator.display.value;
var length=input.length-1;
if(isNaN(input.charAt(length))){
alert("your calculation must end in a digit");
}else{equals();}
}
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum