Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Javascript or jQuery - Check field contains numbers AND letters

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

610 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