Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: validity check

  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Posts
    436
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Aug 2002
    Location
    São Paulo, Brazil
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think you have to add "_" to valid:
    Code:
    var valid = " ABCDEFGHIJKLMNOPQRSTUVWZYZ"
              + "abcdefghijklmnopqrstuvwxyz0123456789_";
    /Daniel

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Posts
    436
    Thanks
    1
    Thanked 0 Times in 0 Posts
    it works great

    thanks

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

  • #4
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Hi,

    one solution:

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

    that's a double quote embedded within single quotes.

    Vinny
    Where the world once stood
    the blades of grass
    cut me still

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,318
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    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_].

  • #6
    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
    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. ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •