www.webdeveloper.com
Results 1 to 8 of 8

Thread: Help with validation

  1. #1
    Join Date
    Jul 2010
    Posts
    3

    Exclamation Help with validation

    I am trying to save my self from writing alot of code. is there anyway that i can put a variable in a document.formname.field.value statement?

    var fieldname = "name_of_field_in_form";
    var fname = document.register.fieldname.value;
    if (fname == "")
    {
    alert("Please enter your first name.");
    return false;
    }
    why doesnt this work^

  2. #2
    Join Date
    Jun 2007
    Posts
    667
    Code:
    var fname = document.forms.register[ "name_of_field_in_form" ].value;
    
    if( !/[a-z]{2}/i.test( fname ) )
    {
     alert("Please enter your first name.");
     return false;
    }
    Y_U U_G_A_E_U_ B_S_A_D_

  3. #3
    Join Date
    Jun 2008
    Posts
    270
    Just out of curiousity, why doesn't this work??
    Code:
    if (fname == "")
    Your solution seems to only check if the value has characters specified in your code in it; not if the value is empty.

    Code:
    if( !/[a-z]{2}/i.test( fname ) )
    {
     alert("Please enter your first name.");
     return false;
    }
    I am not knocking your code (it is beautiful), but I am merely confused why you cannot ask if a variable's value is empty.

  4. #4
    Join Date
    Jun 2007
    Posts
    667
    Quote Originally Posted by bradleybebad View Post

    I am not knocking your code (it is beautiful), but I am merely confused why you cannot ask if a variable's value is empty.
    You can, but what if the field contains " "?. It's not empty but it's not what's expected either.
    Y_U U_G_A_E_U_ B_S_A_D_

  5. #5
    Join Date
    Jun 2008
    Posts
    270
    Will you look at my recent post and see if you can help me? One is a Regex issue and the other is a validation problem (not the email validation). Thanks for the great answer!

    OH YEAH!!! And how would you do if not letters and numerals?
    Last edited by bradleybebad; 07-21-2010 at 12:42 PM.

  6. #6
    Join Date
    Jul 2010
    Posts
    3
    thanks for the replies. i will edit that code. but my original question was if i can use a variable to set the form element being tested.

    ex. if (document.myform.VARIABLEHERE.value == "")
    {
    }

    thanks in advance i appreciate the help!

  7. #7
    Join Date
    Jul 2010
    Posts
    3
    thanks for your help this is what worked for me.

    var fname = document.register.elements['first_name_'+postfix].value;
    if (fname == "")
    {
    alert("Please enter your first name.");
    return false;
    }

  8. #8
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    one must remember that all these client-side validations can be easily falsified. it is much better to think about the server-side validation
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles