Solved

Javascript or jQuery - Check field contains numbers AND letters

Posted on 2009-07-06
3
545 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
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now