More questions about search function on regular expressions

N

.Net Sports

I posed this problem last month here, but need more of a
clarification, as I still cannot correctly flag a bad password. I need
users to submit a passowrd that has at least one numeric character,
along with any amount of string-alpha characters (8 numeric characters
and 1 string character is fine, for example), but having trouble with
the correct regex, as the below still doesnt raise the error alert
when all strings with no numbers are entered:

<script language="JavaScript" type="text/javascript">
function validatePassword(password) {
var error = "";
var illegalChars = /[\W_]/; // allow only letters and numbers

if (password.value == "") {
password.style.background = 'Yellow';
error = "You didn't enter a password.\n";
} else if ((password.value.length < 7) || (password.value.length >
15)) {
error = "The password is the wrong length. \n";
password.style.background = 'Yellow';
} else if (illegalChars.test(password.value)) {
error = "The password contains illegal characters.\n";
password.style.background = 'Yellow';
} else if (!((password.value.search(/[A-Z]+[a-z]+[0-9]/)))){ // &&
(password.value.search(/(0-9)+/)))) {
error = "The password must contain at least one numeral.\n";
password.style.background = 'Yellow';
} else {
password.style.background = 'White';
}
return error;
}

</script>

Thanks in advance
netsports
 
E

Evertjan.

..Net Sports wrote on 08 mei 2007 in comp.lang.javascript:
I posed this problem last month here, but need more of a
clarification, as I still cannot correctly flag a bad password. I need
users to submit a passowrd that has at least one numeric character,
along with any amount of string-alpha characters (8 numeric characters
and 1 string character is fine, for example), but having trouble with
the correct regex, as the below still doesnt raise the error alert
when all strings with no numbers are entered:



<script type="text/javascript">

do not use: language="JavaScript"
function validatePassword(password) {

make sure password is the right object by using the "this" keyword
password.style.background = 'Yellow';

yellow , no uppercase

if (!((password.value.search(/[A-Z]+[a-z]+[0-9]/)))){ // &&
(password.value.search(/(0-9)+/)))) {

I don't understand these last two lines with // and two { {
password.style.background = 'White';

white

================

Try this, much simpler code:

<script type="text/javascript">

function validatePassword(password) {
password.style.backgroundColor = 'yellow';
if (password.value == '')
return 'You didn't enter a password.\n';
if ((password.value.length < 7) ||
(password.value.length >15))
return 'The password is of the wrong length.\n';
if (/[\W_]/.test(password.value))
return 'The password contains illegal characters.\n';
if (/\d/.test(password.value))
return 'The password must contain at least one numeral.\n';
password.style.backgroundColor = 'white';
return "";
}

</script>
 
E

Evertjan.

Evertjan. wrote on 09 mei 2007 in comp.lang.javascript:
Try this, much simpler code:

<script type="text/javascript">

function validatePassword(password) {
password.style.backgroundColor = 'yellow';
if (password.value == '')
return 'You didn't enter a password.\n';
if ((password.value.length < 7) ||
(password.value.length >15))
return 'The password is of the wrong length.\n';
if (/[\W_]/.test(password.value))
return 'The password contains illegal characters.\n';
if (/\d/.test(password.value))

if (!/\d/.test(password.value))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top