1

I don't understand why my is not giving the value properly to my javascript variable. I have tried countless times, changing IDs and whatnot... still nothing... I searched everywhere for an answer... Please help

HTML SNIPPET

<form action="" method="post" onsubmit= "return validateForm()" class="registerform">
First Name: <input type="text" name="fname" id="fname"><br/>
Last Name: <input type="text" name="lname" id="lname"><br/>
Email: <input type="text" name="email" id="email"><br/>
Phone Number: <input type="text" name="phone" id="phone"><br/>
Password: <input type="password" name="password" id="myPass"><br/>
Confirm Password: <input type="password" name="confirmpassword" id="confirmPass"><br/>
<p><input type="submit" value="Submit"></p>
</form>

register.js

...

    var password = document.getElementById("myPass").value;
    var confirmPassword = document.getElementById("confirmPass").value;

...

    function validateForm()
    {
        alert(password);
        alert(confirmPassword);
        error = '';
        var fNamePass = validateFName();
        var lNamePass = validateLName();
        var emailPass = validateEmail();
        var phonePass = validatePhone();
        var passwordPass = validatePassword();
        var passwordConfirmPass = validateConfirmationPassword();

...

in my alerts, the value keeps showing as null. Firebug shows me the value at null... I'm not new to JS, but I feel like I'm doing something really silly here, but my mind is not catching it...

AndroidNoob
  • 219
  • 1
  • 2
  • 6
  • Please no jQuery and please no "variable error is not defined". I put "..." for a reason :p – AndroidNoob Feb 13 '14 at 03:27
  • possible duplicate of [Why does jQuery or a DOM method such as \`getElementById\` not find the element?](http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – Bergi Feb 13 '14 at 03:31
  • You should see that's not the issue – AndroidNoob Feb 13 '14 at 03:34
  • @Bergi, if it wasn't getting the element he would be getting a `cannot read property value of null` – Patrick Evans Feb 13 '14 at 03:35
  • AndroidNoob we are trying to help you here, I tested your code and moving the definition of your variables print them. Please be less arrogant and read the answers. – Jeremy D Feb 13 '14 at 03:36
  • 1
    Ooops, right. I didn't see the `.value`. Also, `null` is an odd value. If it would refer to the properties from above, one would at least have expected `undefined` - though input `.value` always returns a string. As most of the answers have pointed out, it's odd you're accessing the `.value` property outside of the handler function (and therefore not getting the current value), but still one would not have expected `null`. If you can't do the debugging on your own, please provide the full code or a working demo. – Bergi Feb 13 '14 at 03:37
  • can you show more of your code, as Bergi mentions you getting `null` is odd, either something is modifying the variable `password` before your validate function runs or there is something else wonky going on. – Patrick Evans Feb 13 '14 at 03:43
  • You need to provide code that actually reproduces the problem. A complete (but hopefully minimal) document that behaves as you described in your text. – Jukka K. Korpela Feb 13 '14 at 06:33

4 Answers4

1

I think you are running into a scoping issue. You need to make sure you pass the 'password' and 'confirmPassword' argument on to the function.

Ruben
  • 1,296
  • 3
  • 16
  • 24
0

Fine... do it the same way you did the rest

function validateForm()
{

    var password = validatePassword();
    var confirmPassword = validateConfirmPassword();
    alert(password);
    alert(confirmPassword);
    error = '';
    var fNamePass = validateFName();
    var lNamePass = validateLName();
    var emailPass = validateEmail();
    var phonePass = validatePhone();
    var passwordPass = validatePassword();
    var passwordConfirmPass = validateConfirmationPassword();
Seth McClaine
  • 6,298
  • 4
  • 32
  • 53
-1

If I'm understanding the problem correctly, I would assume it's because the input elements don't have a value attribute set yet. Try setting a default value="" and then see if you get a blank value instead of null back.

KaibutsuX
  • 115
  • 7
  • his validation function is called onsubmit so it should be filled in by then, even then you still get an empty string when getting a value from an input that doesnt have a value attribute – Patrick Evans Feb 13 '14 at 03:33
-1

You are setting the value of password and confirmpassword at time of load of document.

Move your definition of password and confirmpassword inside the function

var password, confirmPassword;
function validateForm()
{
    password = document.getElementById("myPass").value;
    confirmPassword = document.getElementById("confirmPass").value;
    alert(password);
    alert(confirmPassword);
Seth McClaine
  • 6,298
  • 4
  • 32
  • 53