Jump to content

how to code this in javascript


Reaper0167

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/140591-how-to-code-this-in-javascript/
Share on other sites

<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>

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.

<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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.