?
Solved

Javascript or jQuery - Check field contains numbers AND letters

Posted on 2009-07-06
3
Medium Priority
?
551 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 900 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 600 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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article discusses how to create an extensible mechanism for linked drop downs.
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 how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

762 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