create an array of field to test if they are null

Discussion in 'Javascript' started by SAN CAZIANO, Oct 6, 2004.

  1. SAN CAZIANO

    SAN CAZIANO Guest

    is there a function that get the name of the first input field of the
    current form ?
    in my example below I want create an array of form field name and in the
    onsubmit assign all element's name to create a simple iteration to test if
    some elements in my array, that must be required, are null:
    something like function verify(array of string) and in onsubmit something
    like return onsubmit(field1,field2,field3....)

    <HTML>
    <HEAD>
    <TITLE>Confirm Dialog Test</title>

    <SCRIPT LANGUAGE=JAVASCRIPT>
    function verify()
    {
    if(document.forms[0].GetElementById[0].value=="")
    {
    alert("Please enter a value in the field");
    return false;
    }
    else{
    return true;
    }
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM onSubmit="return verify()">
    Name: <INPUT TYPE=TEXT NAME="myName"><BR>
    <INPUT TYPE=SUBMIT VALUE="Submit">
    </FORM>
    </BODY>
    </HTML>
    SAN CAZIANO, Oct 6, 2004
    #1
    1. Advertising

  2. On Wed, 06 Oct 2004 14:13:21 GMT, SAN CAZIANO <> wrote:

    > is there a function that get the name of the first input field of the
    > current form ?


    No, but you can use the elements collection to get form controls by
    ordinal number:

    formObj.elements[0] // first element
    formObj.elements[1] // second, etc...

    and from that you can get the name or id:

    formObj.elements[0].name or .id

    However, that isn't really necessary for what you (seem to) want to do.

    > in my example below I want create an array of form field name and in the
    > onsubmit assign all element's name to create a simple iteration to test
    > if some elements in my array, that must be required, are null:
    > something like function verify(array of string) and in onsubmit
    > something like return onsubmit(field1,field2,field3....)


    If you use

    <form ... onsubmit="return verify(this)">

    function verify(form) {

    you can use the elements collection, as I demonstrated above.

    To check that the first control has a value:

    function verify(form) {
    if('' == form.elements[0].value) {
    alert('Please enter a value');
    return false;
    }
    }

    If you want to actually use the name of the control, substitute the number
    with a string containing that name:

    if('' == form.elements['myName'].value) {

    [snip]

    > <SCRIPT LANGUAGE=JAVASCRIPT>


    Don't use the language attribute any more. Not only is it deprecated, but
    the required type attribute makes it redundant.

    <script type="text/javascript">

    [snip]

    I hope that helps,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
    Michael Winter, Oct 6, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    26,479
    Mike Schilling
    Mar 29, 2006
  2. Asfand Yar Qazi
    Replies:
    4
    Views:
    383
    Asfand Yar Qazi
    Nov 12, 2004
  3. Martin M.
    Replies:
    4
    Views:
    334
    Simon Brunning
    Dec 15, 2005
  4. Dave Rudolf
    Replies:
    1
    Views:
    291
    Kai-Uwe Bux
    May 17, 2006
  5. David A. Black
    Replies:
    2
    Views:
    213
    Tim Hunter
    Aug 19, 2004
Loading...

Share This Page