create an array of field to test if they are null

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SAN CAZIANO

    create an array of field to test if they are null

    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>Confir m Dialog Test</title>

    <SCRIPT LANGUAGE=JAVASC RIPT>
    function verify()
    {
    if(document.for ms[0].GetElementById[0].value=="")
    {
    alert("Please enter a value in the field");
    return false;
    }
    else{
    return true;
    }
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM onSubmit="retur n verify()">
    Name: <INPUT TYPE=TEXT NAME="myName">< BR>
    <INPUT TYPE=SUBMIT VALUE="Submit">
    </FORM>
    </BODY>
    </HTML>


  • Michael Winter

    #2
    Re: create an array of field to test if they are null

    On Wed, 06 Oct 2004 14:13:21 GMT, SAN CAZIANO <alberto.kalb@t in.it> wrote:
    [color=blue]
    > is there a function that get the name of the first input field of the
    > current form ?[/color]

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

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

    and from that you can get the name or id:

    formObj.element s[0].name or .id

    However, that isn't really necessary for what you (seem to) want to do.
    [color=blue]
    > 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. ...)[/color]

    If you use

    <form ... onsubmit="retur n 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]
    [color=blue]
    > <SCRIPT LANGUAGE=JAVASC RIPT>[/color]

    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.

    Comment

    Working...