Solved

Javascript or jQuery - Check field contains numbers AND letters

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

840 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