Reaper0167 Posted January 12, 2009 Share Posted January 12, 2009 i have two text fields... password and password confirmation..... i know how to use php to check that both fields are the same and display the error.... i also know that this thread is not posted in the right section but the javascript section seems,, well,,, it just seems like no one is ever there..... question is what is a good way to to check two fields are the same using javascript... i can use javascript to make sure that the fields have something in it...... but i'm a little confused on the fields being the same. Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted January 12, 2009 Share Posted January 12, 2009 The same way? var password = "test"; var password2 = "test"; if(password == password2) { document.write("passwords match"); }else { document.write("passwords do not match"); } Sometimes it helps to just give it a shot Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 12, 2009 Share Posted January 12, 2009 <html> <head> <script type="text/javascript"> function validate(formObj) { if (formObj.password_1.value != formObj.password_2.value) { alert('The passwords do not match!'); return false; } else { alert('The passwords Match. Good job.'); return true; } } </script> </head> <body"> <form name="theform" onsubmit="return validate(this);"> Password: <input type="password" name="password_1"><br> Confirm: <input type="password" name="password_2"><br> <button type="submit">Submit</button> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Reaper0167 Posted January 13, 2009 Author Share Posted January 13, 2009 This is what I have for checking that the fields have atleast something filled in. <script type="text/javascript" language="javascript"> function validate(form2) { var valid = true; if (form2.username.value) { document.getElementById('username_error').innerHTML = ''; } else { document.getElementById('username_error').innerHTML = 'Please enter a username.'; valid = false; } return valid; } </script> then next to my textfield is where my error message comes up.. here is the code for that.. <span id="username_error" class="error"> How would I incorporate that method for checking that both fields are the same. Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 13, 2009 Share Posted January 13, 2009 <script type="text/javascript" language="javascript"> function validate(form2) { var valid = true; if (form2.username.value) { document.getElementById('username_error').innerHTML = ''; } else { document.getElementById('username_error').innerHTML = 'Please enter a username.'; valid = false; } if (!form2.password.value || !form2.confirm.value) { document.getElementById('password_error').innerHTML = 'You must enter a password and confirm it.'; valid = false; } else if (form2.password.value != form2.confirm.value) { document.getElementById('password_error').innerHTML = 'Passwords do not match.'; valid = false; } else { document.getElementById('password_error').innerHTML = ''; } return valid; } </script> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.