Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-18-2003, 02:53 PM   PM User | #1
ShMiL
Regular Coder

 
Join Date: Jul 2002
Posts: 436
Thanks: 1
Thanked 0 Times in 0 Posts
ShMiL is an unknown quantity at this point
validity check

I use this:
Code:
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
ShMiL is offline   Reply With Quote
Old 07-18-2003, 03:52 PM   PM User | #2
Danne
Regular Coder

 
Join Date: Aug 2002
Location: São Paulo, Brazil
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
Danne is an unknown quantity at this point
I think you have to add "_" to valid:
Code:
var valid = " ABCDEFGHIJKLMNOPQRSTUVWZYZ"
          + "abcdefghijklmnopqrstuvwxyz0123456789_";
__________________
/Daniel
Danne is offline   Reply With Quote
Old 07-19-2003, 11:17 AM   PM User | #3
ShMiL
Regular Coder

 
Join Date: Jul 2002
Posts: 436
Thanks: 1
Thanked 0 Times in 0 Posts
ShMiL is an unknown quantity at this point
it works great

thanks

can you tell me how to put a (") in a string, so I can make it valid char...
Thanks
ShMiL is offline   Reply With Quote
Old 07-19-2003, 04:21 PM   PM User | #4
Vincent Puglia
Regular Coder

 
Join Date: Jul 2003
Location: where the World once stood
Posts: 256
Thanks: 0
Thanked 2 Times in 2 Posts
Vincent Puglia is an unknown quantity at this point
Hi,

one solution:

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

that's a double quote embedded within single quotes.

Vinny
__________________
mod at WebXperts
Vincent Puglia is offline   Reply With Quote
Old 07-19-2003, 04:43 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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_].
Philip M is offline   Reply With Quote
Old 07-20-2003, 02:41 AM   PM User | #6
whammy
Senior Coder

 
Join Date: Jun 2002
Location: 41° 8' 52" N -95° 53' 31" W
Posts: 3,660
Thanks: 0
Thanked 0 Times in 0 Posts
whammy is an unknown quantity at this point
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.
__________________
Former ASP Forum Moderator - I'm back!

If you can teach yourself how to learn, you can learn anything. ;)
whammy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:45 PM.


Advertisement
Log in to turn off these ads.