Validation Problems..

Discussion in 'JavaScript' started by adamjblakey, Nov 1, 2007.

  1. #1
    Hi,

    I am trying to validate the email address, username and password in a form i have. I have got it so that it checks to make sure that there is something in the field but i need the email field to check for @ symbol and also i need to make sure that the username and password entries are between 4 and 12 characters.

    Here is my code up to now.

    
    function checkform ( form )
    {
    
     if (form.email.value == "") {
        alert( "Please ensure your email addresses have been input correctly e.g. mail@mail.com" );
        form.email.focus();
        return false ;
      }
    
      if (form.username.value == "") {
        alert( "Please enter a username between 4 - 12 characters" );
        form.username.focus();
        return false ;
      }
    
      if (form.password.value == "") {
        alert( "Please enter a password between 6 - 12 characters" );
        form.password.focus();
        return false ;
      }
    
      return true ;
    }
    
    
    Code (markup):
    Cheers,
    Adam
     
    adamjblakey, Nov 1, 2007 IP
  2. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    for username:
    if (form.username.value.length < 4 || form.username.value.length > 12) { ... }

    similarly for password.

    for email, if you simply want to check that there's an '@' in the middle of the string:
    if (!form.email.value.match(/.+@.+)) { ... }
    for more complex email validation, a quick search on your preferred search engine should show you some examples.
     
    phper, Nov 1, 2007 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Thanks for that :)

    It does not seem to be working though, it does not check for these now and just enters without anything entering anything.


     
    
    function checkform ( form )
    {
    
    
      if (!form.email.value.match(/.+@.+)) {
        alert( "Please ensure your email addresses have been input correctly e.g. mail@mail.com" );
        form.email.focus();
        return false ;
      }
    
     if (form.username.value.length < 4 || form.username.value.length > 12) {
        alert( "Please enter a username between 4 - 12 characters" );
        form.username.focus();
        return false ;
      }
    
      if (form.password.value.length < 4 || form.password.value.length > 12) {
        alert( "Please enter a password between 6 - 12 characters" );
        form.password.focus();
        return false ;
      }
    
      // ** END **
      return true ;
    }
    Code (markup):
    Any ideas?
     
    adamjblakey, Nov 2, 2007 IP
  4. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    try !form.email.value.match(/^\.+@\.+$/)

    for a slightly more advanced e-mail checker..
    !form.email.value.match(/^\w+(-\w*)*@[a-zA-Z_]+(\.[a-zA-Z_]+)*\.[a-zA-Z]{2,3}$/)

    also, you might want to use regular expressions on your password and username fields as well.. this can make it so you can restrict the characters allowed..

    !form.username.value.match(/^\w{4,12}$/)

    !form.password.value.match(/^\w{4,12}$/)

    these will only allow word characters
     
    Jamie18, Nov 2, 2007 IP
  5. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #5
    Sorry, I forgot an ending slash in the email validation.
    It's supposed to be:
    if (!form.email.value.match(/.+@.+/))
     
    phper, Nov 4, 2007 IP
  6. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Good job in adding the trailing forward slash, but the backslashes in front of the dots shouldn't be there. Otherwise it'd be saying "one or more dots followed by an '@', then followed by one or more dots.

    More advanced indeed. Just a few small problems though:
    - Email addresses with a dot in it (e.g.: ) would be considered invalid. There are also other valid, non-alphanumeric characters for the part to the left of the '@', which would fail this validation.
    - Emails with numbers in the domain name (e.g.: ) would also fail this validation.
    - It allows underscores in domain names, but not dashes. It should be the other way around.

    But yeah, with a few improvements, this would make a much stricter validation.
     
    phper, Nov 4, 2007 IP