-2

I am struggling to get some JavaScript code together that would take in a value from a html input box and would return true if it contained both a letter (A-Z or a-z) and a digit (0-9) in any order.

So A1, a1, 1a, 1A, aAaAaA1, 111111a etc would all return true.

aa and 11 would return false because they only contain one group and not both.

Can anyone suggest how I could make this happen in JavaScript? I'm just stuck at how JavaScript would evaluate the value... everything else is working.

Thanks!

FMC
  • 650
  • 12
  • 31

3 Answers3

3

    str = "llf2ds";
    var re = /(?=.*\d)(?=.*[a-zA-Z])/;
    alert(re.test(str))
Sampath Liyanage
  • 4,776
  • 2
  • 28
  • 40
  • Hi Sampath, thanks for the reply. Using your regex it still doesnt appear to be working. I have "else if (!pass1.value.match("/^(?=.*\d)(?=.*[a-zA-Z])$/")) {..." but it is entering this even with letters and numbers. – FMC Dec 16 '14 at 19:28
  • Thank you so much! I have been trying to get this to work since yesterday. – FMC Dec 16 '14 at 19:33
1

I think you should do something like this:

function foo(inpval) {

    var letmatch = inpval.match(/[a-z]/g);
    var digmatch= inpval.match(/[0-9]/g);

    if(letmatch && digmatch) {
        return true;
    }
    return false;
}
Tornike
  • 1,245
  • 12
  • 35
0

I believe this might be useful: http://www.the-art-of-web.com/javascript/validate-password/

Validation, that passwords must contain at least six characters, including uppercase, lowercase letters and numbers.