Solved

Javascript or jQuery - Check field contains numbers AND letters

Posted on 2009-07-06
3
550 Views
Last Modified: 2012-05-07
Hi. I need to check the value entered in a password field contains both letters and numbers.
If the user enters just letters, it needs to throw up an error.
If its just numbers, again, an error.
So some examples:

Password: 43242  = ERROR
Password: deef = ERROR
Password: deef2 = CORRECT

I need the code to be as simple as possible, I dont want to use a pre-defined validation script. Something like..

$("#password").blur(function () {
      var b = document.getElementById("#password").value;
      if(b = NUMBERANDLETTER) {
            alert("Password is valid");
                  }
            else {
                  alert("You need to enter numbers and letters");
            }
});

OBVIOUSLY this code is incorrect, but I need it to be as simple as possible.

Need asap!

Thanks!
0
Comment
Question by:callum1002
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 9

Accepted Solution

by:
Shahzad Fateh Ali earned 300 total points
ID: 24784524
Try this

$("#password").blur(function () {
      var b = document.getElementById("#password").value;
      var pp = /(([a-z][A-Z])+[0-9]+)+/i
      if(pp.test(b)) {
            alert("Password is valid");
                  }
            else {
                  alert("You need to enter numbers and letters");
            }
});

Open in new window

0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 200 total points
ID: 24784658
try

http://nilangshah.wordpress.com/2007/06/26/password-validation-via-regular-expression/

var reg = ^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*$
.
.
 
   if(reg.test(b)) {
            alert("Password is valid");
                  }
            else {
                  alert("You need to enter numbers and letters");
            }

Open in new window

0
 

Author Closing Comment

by:callum1002
ID: 31600115
Thanks, it worked however the getElementById("#password") should have been getElementById("password") i belive. Got it working anyway, thanks! :)
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question