Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help validating a text box.

    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

  • #2
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post
    i did not get this:
    only a numerical value can be the last digit input into the display when the submit button is clicked.

  • #3
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post
    Quote Originally Posted by bhakti_thakkar View Post
    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 ?

  • #4
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    if you are wanting to check only the last character of he input do as so:


    Code:
    <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>
    Last edited by brandonH; 12-04-2006 at 12:21 PM.
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

  • #5
    New Coder
    Join Date
    Nov 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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();"

    Code:
    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")
    
    }
    Last edited by supacook2k; 12-05-2006 at 11:24 PM.

  • #6
    New Coder
    Join Date
    Nov 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i've updated my last post. sort of got it working but got a problem. listed problem in last post.

  • #7
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    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.
    Code:
    <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>
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •