How do I get form data for validating forms

Discussion in 'JavaScript' started by Imozeb, Jan 28, 2010.

  1. #1
    I have created a form with 5 text field for users to enter information. I want to validate my form before I send it to my server. When I search for how to validate forms using javascript it only comes up with how to validate 1 text field of the form. Since I have 5 fields I need a way of validating more then one field. How could I do that or how to do get the value in javascript of 1 of the form objects? I know there is a .value script but it doesn't seem to work with more than 1 form object.

    My javascipt looks something like this:

    <script type="text/javascript">
    function validate_newuser_form(form)
    {
    with (form)
    {
    if (username.value==null||username.value=="")
    {
    username.focus();return false; alert("User name validation failed, please enter a value!");
    }
    else
    {
    return true;
    }
    }
    }
    </script>

    and the html form looks like this:

    <form action="" onsubmit="return validate_newmember_form(this)" method="post" name="newuser" dir="ltr">
    <input name="username" type="text" id="username" value="" size="31" maxlength="30" />
    <input name="newpassword" type="text" id="newpassword" size="21" maxlength="20" />
    <input name="confirmpassword" type="text" id="confirmpassword" size="21" maxlength="20" />
    <input name = "submit" type="submit" id="submit"/>
    </form>

    Thanks.

    ~Imozeb
     
    Imozeb, Jan 28, 2010 IP
  2. harrierdh

    harrierdh Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There is no need to pass this to your function. Javascript can pull out any variable using getElementByID assuming of course that your tag has a unique id. Fortunately you do.

    <script>
    function validate_newmember_form() {
    var username = document.getElementById("username").value;
    var newpassword = document.getElementById("newpassword").value;
    var confirmpassword = document.getElementById("confirmpassword").value;
    if (username == "") {
    alert("You have not entered a user name");
    return false;
    }
    if (newpassword != confirmpassword) {
    alert("Your passwords do not match");
    return false;
    }
    }

    <form action="" onsubmit="return validate_newmember_form()" method="post" name="newuser" dir="ltr">
    <input name="username" type="text" id="username" value="" size="31" maxlength="30" />
    <input name="newpassword" type="text" id="newpassword" size="21" maxlength="20" />
    <input name="confirmpassword" type="text" id="confirmpassword" size="21" maxlength="20" />
    <input name = "submit" type="submit" id="submit"/>
    </form>
     
    harrierdh, Jan 28, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks harrierdh.

    ~Imozeb
     
    Imozeb, Jan 28, 2010 IP