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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.