SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile substract numbers in text fields

    Hi everyone

    I don't know anything about javascript, that is why I'm asking you for your help
    Actually, I need to be able to catch the number in textfield1 and substract the number in textfield2 to it.
    Is it possible to display a message if my textfields are not filled in with numbers?

    Here is my HTML code :

    <input type="text" name="textfield" />
    </label>
    to which you substract
    <label>
    <input type="text" name="textfield2" />
    </label>


    Thanks a lot for your help, have a nice day

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should help you on the way:

    <script>
    function test()
    {
    var t1 = document.getElementById('textfield').value;
    var t2 = document.getElementById('textfield2').value;

    if (t1 == "" || t2 == "" || isNaN(t1) || isNaN(t2)) alert("no numbers!");

    }
    </script>
    <input type="button" onclick="test()" value="test"/>
    Teun Hoogendoorn
    ATSC
    LinkedIn
    Blog

  3. #3
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much !!!

  4. #4
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've tried this but my browser tells me the function "test" is undefined. Can you tell me how to proceed, and then how to substract the values?
    Thanx

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    <html>
    <head>
    <script>
    function test()
    {
    var t1 = document.getElementById("textfield").value;
    var t2 = document.getElementById("textfield2").value;

    var n1 = parseInt(t1);
    var n2 = parseInt(t2);

    if (t1 != n1 || t2 != n2) alert("no numbers!");
    else alert(t1 - t2);

    }
    </script>
    </head>
    <body>
    <input type="text" id="textfield" name="textfield" />
    to which you substract
    <input type="text" id="textfield2" name="textfield2" />
    <input type="button" onclick="test()" value="test"/>
    </body>
    </html>
    Teun Hoogendoorn
    ATSC
    LinkedIn
    Blog

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Try defining the script tag properly

    <script type="text/javascript">
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •