• 0

Javascript form validation


Question

Hello I have been working for some time now to try and get this form set just the way I want.

I would like to validate each field. I have it set up with the exception of two parts. I don't know how to create the pattern for the field to compare against and I cannot get my password boxes to give an error when the confirm box is not the same as the password box. I have tried this two different methods to get this part to work with no success:

Method 1:

var password = document.getElementById('password');

var password_confirm = document.getElementById('password_confirm');

if(password != password_confirm)

window.alert("The passwords do not match");

------------------------------------------------------------------------------------------------------------

Method 2:

if(document.getElementById('password') != document.getElementById('password_confirm'))

window.alert("The passwords do not match");

any help is appreciated.

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Use document.getElementById().value or password.value/password_confirm.value to compare the values. By using getElementById, you're getting the element object, which has numerous properties, including value, which is what you want.

Link to comment
Share on other sites

  • 0

Use document.getElementById().value or password.value/password_confirm.value to compare the values. By using getElementById, you're getting the element object, which has numerous properties, including value, which is what you want.

I tried that as well with no luck.

Link to comment
Share on other sites

  • 0

Got a link to the page? If document.getElementById('password').value doesn't return a value, then that tells me there is no form element with an id of password (and password_confirm for that matter).

If not a link to the page, then perhaps posting the code being used?

Link to comment
Share on other sites

  • 0

Got a link to the page? If document.getElementById('password').value doesn't return a value, then that tells me there is no form element with an id of password (and password_confirm for that matter).

If not a link to the page, then perhaps posting the code being used?

Gotta love those simple mistakes I had the name set but did not have an id set. It's working fine now. Thanks.

Now...what about creating the pattern for the other fields?

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.