javascript combine question

Discussion in 'JavaScript' started by GULLIVER, Feb 23, 2012.

  1. #1
    Hello,

    Will appreciate a quick help hear if you can.

    I have 3 inbut box values (strings) from a form -

    1. form.box1.value
    2. form.box2.value
    3. form.box3.value


    I want to declare a variable (var) that'll combine all the above 3 values into one string.

    var = finalstring tell me the code;

    Help is appreciated..

    thanks
     
    GULLIVER, Feb 23, 2012 IP
  2. StangMan23

    StangMan23 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    var finalstring = form.box1.value + ' ' + form.box2.value + ' ' + form.box3.value;

    You'll probably want to wait and do this AFTER the user has filled out the form box by calling it when onsubmit happens with the form.
     
    StangMan23, Feb 24, 2012 IP
  3. GULLIVER

    GULLIVER Well-Known Member

    Messages:
    668
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Thanks StangMan23 - how exactly can I wait to do that AFTER the user has filled out the form?

    Thanks
     
    GULLIVER, Feb 24, 2012 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    In each input box's onBlur() function, check to see that each box is not empty. When each one has something, set your variable.

    Or, in your form validation, tell the user to fill in any empty box. If validation gets past that point (no empty boxes), set the variable, then do whatever else you need to do to validate the form.
     
    Rukbat, Feb 25, 2012 IP
  5. suwandichen13

    suwandichen13 Well-Known Member

    Messages:
    618
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    140
    #5
    i still dont get it..can you guys explain me??thanks..
    with examples please.
     
    suwandichen13, Jul 3, 2012 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    This would check the lengths of the text entered into your text boxes before the form submits. If a box is empty, the user gets an alert and the form won't submit. (You can test boxes for valid data here too - phone numbers - minus dashes, spaces and parentheses - should contain only 0-9, you can use a regex to test email addresses, etc.)

    
    <javascript>
    function checkLengths() {
        var l = document.getElementById("nameId").value;
        if(l.replace(/^\s+|\s+$/g,"").length == 0) {
            alert('You have to enter a name.');
            return false;
        } else {
        l = document.getElementById("addressId").value;
        if(l.replace(/^\s+|\s+$/g,"").length == 0) {
            alert('You have to enter an address.');
            return false;
        }
        //more tests
        return true;
    }
    </javascript>
    
    <form action="submit.htm" onSubmit="checkLengths()">
        Name: <input type="text" id="nameId" />
        //etc.
    
    Code (markup):
    (No guarantee that the code is bug-free - I just whipped it out fast.)
     
    Rukbat, Jul 4, 2012 IP
  7. Irfi0009

    Irfi0009 Banned

    Messages:
    17,584
    Likes Received:
    33
    Best Answers:
    1
    Trophy Points:
    48
    #7
    i like this form.box1.value
     
    Irfi0009, Jul 11, 2012 IP