...

View Full Version : validity check



ShMiL
07-18-2003, 03:53 PM
I use this:


var valid = "ABCDEFGHIJKLMNOPQRSTUVWZYZabcdefghijklmnopqrstuvwxyz0123456789"
var temp;
var ok = "yes";
for (var i=0; i < document.login.username.value.length; i++) {
temp = "" + document.login.username.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = 'no';
}

if (document.login.username.value.length < 5 || document.login.username.value.length > 15 || ok == 'no'){
alert('not VALID')
document.login.username.select();
return false;}

and it works great
but when I put space ( ) or underline (_) - it seems to return not valid everytime!
what could be the problem?
Thanks

Danne
07-18-2003, 04:52 PM
I think you have to add "_" to valid:


var valid = " ABCDEFGHIJKLMNOPQRSTUVWZYZ"
+ "abcdefghijklmnopqrstuvwxyz0123456789_";

ShMiL
07-19-2003, 12:17 PM
it works great

thanks :thumbsup:

can you tell me how to put a (") in a string, so I can make it valid char...
Thanks :)

Vincent Puglia
07-19-2003, 05:21 PM
Hi,

one solution:

valid = "...789" + '"'

that's a double quote embedded within single quotes.

Vinny

Philip M
07-19-2003, 05:43 PM
You can simplify your code a lot by using a regular expression:-

temp = document.login.username.value
if(/\W/.test(temp)) {
ok == "no";
alert ("Invalid Characters");
}
if (temp <5 || temp >15 {
ok == "no";
alert ("Not Valid");
}
if (ok=="no) {
document.login.username.select();
return false;
}


\w matches any alphanumeric character including the underscore.
It is equivalent to [a-zA-Z0-9_].

\W matches any single non-word character, i.e. NOT [a-zA-Z0-9_].

whammy
07-20-2003, 03:41 AM
Philip M's suggestion is actually what I was about to suggest. No reason to type out the whole alphabet when regular expressions already do it for ya. ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum