Hi all

I want to make sure that my password field should contain numbers as well as letters

i am using something like

function isAlphabet(elem, helperMsg){
    var alphaExp = /^[a-zA-Z]+$/;
    if(elem.value.match(alphaExp)){
        return true;
    }else{
        alert(helperMsg);
        elem.focus();
        return false;
    }
}

but it does nothing when there are letter in field but not numbers, same when contains numbers but not letters.

so , how to make sure it asks for both , i mean i want to force user to enter letters and numbers both as mandate.

thanks in advance,

Recommended Answers

All 4 Replies

try:

function isAlphabet(elem, helperMsg){
    var alphaExp = /[a-z]+/i;
    var nums=/\d+/;
    var alnum=/^[a-z\d]+$/i;
    if(elem.value.match(alnum) && elem.value.match(alphaExp) && elem.value.match(nums) ){
        return true;
    }else{
        alert(helperMsg);
        setTimeout(function(){elem.focus();},20);
    return false;
    }
}

thanks for the reply but can you please check again? it didn't work here.

it seems to work fine for me:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">

function isAlphabet(elem, helperMsg){
    var alphaExp = /[a-z]+/i;
    var nums=/\d+/;
    var alnum=/^[a-z\d]+$/i;
    if(elem.value.match(alnum) && elem.value.match(alphaExp) && elem.value.match(nums) ){
        return true;
    }else{
        alert(helperMsg);
        setTimeout(function(){elem.focus();},20);
    return false;
    }
}
</script>
<input type="text" id="Username" name="username" onchange="isAlphabet(this, this.id+' is not valid. It must contain at least one letter and one number') "/>
</body>
</html>

GREAT! It worked sir.

THANKS a lot! i dont know how to thanks you for your help. i wish if i could serve you coffee or something right now. :-)

Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.