SitePoint Sponsor

User Tag List

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

    Javascript conversion code

    Hi all,

    First of all thanks for everyone who responds to me. I appreciate it.

    Here is my problem.


    I have a HTML form containing two text box controls in it and a submit button. When a user enters information in those two textboxes and click on submit, the information is sent to a function in Javascript. In the javascript, the information from those textboxes is stored in a javascript variable.

    The problem is as follows:

    When I am inputting string text in the html text boxes and in the javascript when I am trying to print those values, it is giving me out an error saying NaN. However when i input integer values in the text boxes it is printing those numbers. Is there a conversion that I have to do for the string to be printed. I am new to Javascript and need your help. This is a basic code of Javascript. Below is the code that I have. Thanks in advance for your help.


    <html>
    <script type = "text/javascript">

    function square(form)
    {


    var test = form.value1.value;
    window.alert(+test);

    var point = document.test.value1.value;
    pointcon = point.toString();

    window.alert("nice");
    window.alert(+pointcon);

    var point2 = eval(document.test.value2.value);

    var point3 = point + point2;
    alert(point3);

    }

    </script>



    <head>
    <body>
    <form name = "test">
    First name: <input type="text" name = "value1" value1 = ""/><br />
    Last name: <input type="text" name = "value2" value2 = ""/>
    <input type = "button" value = "Submit" onClick="square(this.form)"/>
    </form>
    </body>

    </html>

  2. #2
    SitePoint Zealot
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't use eval for this purpose. Use Number, parseInt or parseFloat: https://developer.mozilla.org/en/Cor...loat_Functions
    Also remove the plus from in the alert argument
    Code:
    window.alert(+test);

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Here is an example of the sort of code that you could be using instead:

    Code javascript:
    var test = document.getElementById('test'),
        value1 = test.elements.value1.value,
        value2 = test.elements.value2.value,
        point = Number(value1),
        point2 = Number(value2),
        point3 = point + point2;
    alert(point3);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •