SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help with a function please

    i've got a problem validating my text field. 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 it also alerts when the calculation in the display is e.g. 9+9 which is correct yet alerts saying calculation must end in a digit and then performs the calculation after clicking ok, basically i just want it to alert when the last character is not a number.

    any help greatly appreciated

    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")
    
    }

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by supacook2k View Post
    i've got a problem validating my text field. 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 it also alerts when the calculation in the display is e.g. 9+9 which is correct yet alerts saying calculation must end in a digit and then performs the calculation after clicking ok, basically i just want it to alert when the last character is not a number.

    any help greatly appreciated

    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")
    
    }
    Your if statement does nothing, therefore the alert is not associated with it and will always execute.


Bookmarks

Posting Permissions

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