Form Validation

Discussion in 'JavaScript' started by oo7ml, Jun 18, 2007.

  1. #1
    I have some form validation done, but it's not very good. How do i make sure that users can only use a-z, A-Z, 0-9. (there for users would not be able to use special characters) - note: i have the php server side validation done but i want javascript also, thanks

    Here is the code i have so far:

    function validate_username(field,alerttxt)
    	{
    		with (field)
    			{
    				numofchar=value.length;
    				if (numofchar<5) 
    					  {alert(alerttxt);return false}
    				else {return true}
    			}
    	}
    HTML:
    function validate_form(thisform)
    {
    	with (thisform)
    	{
    
    if (validate_username(username,"Ooops... Username must be at least 5 characters in length")==false)
    		  {username.focus();return false}
    HTML:

    how can i make sure that the $username only can have a-z, A-Z, 0-9
     
    oo7ml, Jun 18, 2007 IP
  2. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #2
    if (numofchar<5 || value.match(/[^a-z0-9]/i))
    Code (markup):
    Also, you might want to update the message so it informs of this new check.
     
    krt, Jun 18, 2007 IP
  3. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #3
    How can i change this line around so that

    if (numofchar<5 || value.match(/[^a-z0-9]/i))
    HTML:
    The rules are:
    between 5 and 15 characters
    a-z A-Z 0-9 characters only

    thanks for your help
     
    oo7ml, Jun 19, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    nico_swd, Jun 19, 2007 IP
  5. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #5
    Sorry, i'm new to all of this, i didn't think you could use the same format in Javascript as you could in PHP. Thanks a million nico_swd, your a big help, thanks
     
    oo7ml, Jun 19, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    No problem. :D
     
    nico_swd, Jun 19, 2007 IP
  7. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #7
    Sorry, i'm after running into a bit of a problem. If i use

    if (!value.match(/^[a-z0-9]{5,15}$/i))
    HTML:
    If a users name is O'Neill it wont accept it becaue of the ' character in the name.

    How do i just change the if statement to only check if its {5,15} or is their a way that i can inlude certain character such as ' and , and -
     
    oo7ml, Jun 19, 2007 IP
  8. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #8
    Add them to the list inside [ and ]. Usually it will work except in a few cases. e.g. a hyphen has to be escaped or at the end of the list:
    if (!value.match(/^[a-z0-9',-]{5,15}$/i))
    Code (markup):
     
    krt, Jun 19, 2007 IP