0

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,

Edited by Dani: Formatting fixed

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by dalip_007
0

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;
    }
}
0

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

0

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>

Edited by hielo: n/a

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.