0

I am trying to validate a user entered password. It can only be letters and numbers and I have to use the match method...even if it isn't the best way...I have to use match(). I am missing something to get the working properly. No number or special characters only letters. I do not know much about javascript.

<script type="text/JavaScript">
    function chkPwd() {
    var pwd = document.form1.pwd;
    var pwdMsg = document.getElementById('pwdMsg'); 

    regex = /[^a-zA-Z]/;
        var pwd1 = pwd.value;
        if(!pwd1.match(regex)) {
            pwdMsg.innerHTML = "Must contain letters only!"
            pwd.select();
            return;
        }else{ 
            pwdMsg.innerHTML = "";
        }
    }   
</script>
</head>

<body>
        <form name="form1" action="" method="post">


            <p> Password: <br>
                <input type="text" name="pwd" onchange="chkPwd()" />
                <span id="pwdMsg"></span></p>
            <p>
                <input type="button" value="chkPass" onclick="chkPwd()">
            </p>
        </form>
        <div id="results"></div>
</body>
</html>
5
  • you defined regex but then used regx which appears to be undefined. Commented Aug 11, 2014 at 0:20
  • I'd imagine if you looked at your javascript console you should have seen the error. Commented Aug 11, 2014 at 0:22
  • Thanks @smerny I see the error in my console..like I said I don't know much about javascript. Which is why I came here to get help. Commented Aug 11, 2014 at 0:26
  • you keep modifying your question with the fixes... do you still have a problem? Commented Aug 11, 2014 at 0:28
  • yes. I still have the same problem. I only modified the problem because people kept pointing out my typo after I said that I saw it. So I fixed it. The problem is that when all letters are entered it doesn't pass validation...which it should. Commented Aug 11, 2014 at 0:31

2 Answers 2

1

Your issue appears to be in this section of code.

regex = /[^a-zA-Z]/;
var pwd1 = pwd.value;
if(!pwd1.value.match(regx)) {

You are setting the pwd1 to pwd.value, but then on the next line, you are accessing pwd1.value. This means that you are efficiently doing pwd.value.value. Additionally, you are using regx where you should use regex. Also, your if condition does not appear to need a ! in it. I think you mean to do this.

regex = /[^a-zA-Z]/;
var pwd1 = pwd.value;
if(pwd1.match(regex)) {
Sign up to request clarification or add additional context in comments.

2 Comments

This is close...however the error message still comes up when it is only letters still when it should be fine.
@floyd See updated answer. The ! in the if statement is the problem.
0

If you want letters only you should use this regex:

^[A-Za-z]+$

If you want to have letters and numbers then use:

^[A-Za-z0-9]+$

Btw, as guys pointed out you are using regx instead of regex

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.