SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    Validate Password

    I need a javascript, that will validate my password, to see if it is alphanumeric, before submitting. I have searched all over, please help.

    Thanks.

  2. #2
    SitePoint Evangelist
    Join Date
    Nov 2002
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Password validation

    The easiest way to accomplish this is with regular expressions. The code below will check if the password is between 6 and 10 characters and if it's alphanumeric:

    <script language="javascript">


    function validatePassword(sPassword){
    var re = /^[A-Za-z0-9]{6,10}$/;
    if (!re.test(sPassword)){
    alert("Please enter a valid password!");
    }else{
    alert("The password you enter is valid!");
    }
    }
    </script>

    <form name="MainForm" method="GET">
    <table>
    <tr>
    <td align="right" class="main">Password/td>
    <td class="main"><input type="password" name="txtPassword"></td>
    </tr>
    <tr>
    <td align="right" class="main">&nbsp;</td>
    <td class="main"><input type="button" value="Validate Password" onclick="validatePassword(this.form.txtPassword.value)"></td>
    </tr>
    </table>
    </form>

  3. #3
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    thanks. Just want I needed.

  4. #4
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    one more thing, if you have the time, how would I make it validate, an email feild, for example, it has to contain @ and a period.

  5. #5
    SitePoint Guru okrogius's Avatar
    Join Date
    Mar 2002
    Location
    US
    Posts
    622
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Client side validation is great, but note that yo should never rely on it. A proper server side validation is a must.

  6. #6
    SitePoint Evangelist
    Join Date
    Nov 2002
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Email validation

    Here is the email validation function

    function validateEmail(strEmail){
    re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    return re.test(strEmail);
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •