PLEASE -- why doesn't this validate function work? PLEASE?

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

    PLEASE -- why doesn't this validate function work? PLEASE?

    Hi, I've tried everything I can think of. Using the script below, I
    thought on submit my ConfirmSave function would run, then if true
    returned, would run my form action.

    At this point it appears to be skipping the function altogether. I'm
    also unable to get the textbox.value since I need to use variable for
    the form name and input element name. I'm fairly new to this...please
    tell me where I am going astray...Thanks .

    HTML element:

    <form name="form_test "
    action="parent. hidden.location =ValidateSave.a spx" method="post"
    onsubmit="retur n ConfirmSave('fo rm_test','measu rement')">
    <input type="text" name="measureme nt" value="{.}"/>
    <input type="submit" value="SAVE"/>
    </form>

    FUNCTION:

    function ConfirmSave(frm ,input) 'arguments get generated via xsl
    'for this example, they are
    (form_test,meas urement)
    {
    var formname="" +frm
    var test="" +input
    var content="" +input.value

    //tested var's with alert box, frm and input ok, but not value!

    if (document.formn ame.test.value= ="")
    {
    alert("You are trying to save a blank entry.");
    formname.test.f ocus();
    formname.test.s elect();
    return false;
    }
    else {
    confirm("You are about to enter: " +formname.test. value+ " -- are
    you sure?");
    return false;
    }
    return true;
    }
  • Grant Wagner

    #2
    Re: PLEASE -- why doesn't this validate function work? PLEASE?

    KathyB wrote:
    [color=blue]
    > Hi, I've tried everything I can think of. Using the script below, I
    > thought on submit my ConfirmSave function would run, then if true
    > returned, would run my form action.
    >
    > At this point it appears to be skipping the function altogether. I'm
    > also unable to get the textbox.value since I need to use variable for
    > the form name and input element name. I'm fairly new to this...please
    > tell me where I am going astray...Thanks .
    >
    > HTML element:
    >
    > <form name="form_test "
    > action="parent. hidden.location =ValidateSave.a spx" method="post"
    > onsubmit="retur n ConfirmSave('fo rm_test','measu rement')">
    > <input type="text" name="measureme nt" value="{.}"/>
    > <input type="submit" value="SAVE"/>
    > </form>
    >
    > FUNCTION:
    >
    > function ConfirmSave(frm ,input) 'arguments get generated via xsl
    > 'for this example, they are
    > (form_test,meas urement)
    > {
    > var formname="" +frm
    > var test="" +input
    > var content="" +input.value
    >
    > //tested var's with alert box, frm and input ok, but not value!
    >
    > if (document.formn ame.test.value= ="")
    > {
    > alert("You are trying to save a blank entry.");
    > formname.test.f ocus();
    > formname.test.s elect();
    > return false;
    > }
    > else {
    > confirm("You are about to enter: " +formname.test. value+ " -- are
    > you sure?");
    > return false;
    > }
    > return true;
    > }[/color]

    It's not working because you can't reference a form with just it's name
    in most browsers. Even in browsers where you can use just the form name,
    document.SomeVa riableContainin gTheNameOfAForm simply does not work.

    What you basically have is:

    var theFormName = 'blah';
    var theInputName = 'bleh';
    document.theFor mName.theInputN ame.value ....

    What you want is:

    var theFormName = 'blah';
    var theInputName = 'bleh';
    document.forms[theFormName].elements[theInputName].value ...

    As well, confirm() returns a boolean true/false depending on what they
    selected, so you are calling confirm(), then returning false all the
    time.

    Basically, there's just a lot of little things that are wrong with the
    script (such as why you are taking paramenters, concatenating an empty
    string to them and assigning them to another variable).

    You already have a reference to the form in the onsubmit event, so just
    use:

    <form ... onsubmit="retur n ConfirmSave(thi s, 'measurement'); ">

    and

    function ConfirmSave(for mReference, fieldName) {

    var theFormField = formReference.e lements[fieldName];

    if (theFormField.v alue == "") {
    alert("You are trying to save a blank entry.");
    theFormField.fo cus();
    theFormField.se lect();
    return false;
    } else {
    return confirm("You are about to enter: " +
    theFormField.va lue +
    " -- are you sure?");
    }
    }

    --
    | Grant Wagner <gwagner@agrico reunited.com>

    * Client-side Javascript and Netscape 4 DOM Reference available at:
    *


    * Internet Explorer DOM Reference available at:
    *
    Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


    * Netscape 6/7 DOM Reference available at:
    * http://www.mozilla.org/docs/dom/domref/
    * Tips for upgrading JavaScript for Netscape 7 / Mozilla
    * http://www.mozilla.org/docs/web-deve...upgrade_2.html


    Comment

    • Kathy Burke

      #3
      Re: PLEASE -- why doesn't this validate function work? PLEASE?

      Thanks Grant. I appreciate your taking the time to explain the "how" to
      me. I'm definitely new at the javascript stuff and your answer helped me
      a lot. Believe it or not, I put together what I have from books and
      postings...it all looked so easy!

      Thanks again,

      Kathy

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • George M Jempty

        #4
        Re: PLEASE -- why doesn't this validate function work? PLEASE?

        KathyB wrote:[color=blue]
        > Hi, I've tried everything I can think of. Using the script below, I
        > thought on submit my ConfirmSave function would run, then if true
        > returned, would run my form action.
        >
        > At this point it appears to be skipping the function altogether. I'm
        > also unable to get the textbox.value since I need to use variable for
        > the form name and input element name. I'm fairly new to this...please
        > tell me where I am going astray...Thanks .
        >
        > HTML element:
        >
        > <form name="form_test "
        > action="parent. hidden.location =ValidateSave.a spx" method="post"
        > onsubmit="retur n ConfirmSave('fo rm_test','measu rement')">
        > <input type="text" name="measureme nt" value="{.}"/>
        > <input type="submit" value="SAVE"/>
        > </form>
        >
        > FUNCTION:
        >
        > function ConfirmSave(frm ,input) 'arguments get generated via xsl
        > 'for this example, they are
        > (form_test,meas urement)
        > {
        > var formname="" +frm
        > var test="" +input
        > var content="" +input.value
        >
        > //tested var's with alert box, frm and input ok, but not value!
        >
        > if (document.formn ame.test.value= ="")
        > {
        > alert("You are trying to save a blank entry.");[/color]

        What if the user enters " "? Isn't that a blank entry too? This is
        the number one mistake I see with roll your own form validation routines
        -- including a lot I've seen in books. This is why I so often recommend
        the routine you can pick up at:

        http://pengoworks.com/index.cfm?action=get:qforms.

        It's kind of like recommending CGI.pm on comp.lang.perl. misc. Though I
        would be interested if anybody has seen anything better, as good, or
        even nearly as good as the above.

        Comment

        Working...