A problem with syntax or something else …?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: A problem with syntax or something else …?

  1. #1
    Join Date
    Dec 2002
    Posts
    8

    Post A problem with syntax or something else …?

    Hello, have just joined today and hope that this is the kind of question that is appropriate???

    1) Wish to add a small script to verify a required field/create a special alert message
    2) Action is triggered by “Submit” and onClick e.g.
    <input type=button value=”Click here to finalise your order” onclick=”verify();”>

    Problem:
    “Submit” and onClick already refers to another function e.g.
    <input type=button value=”Click here to finalise your order” onclick=” WriteCustInfo();”>

    3) Is there a way, please, of triggering both “verify();” and “WriteCustInfo();” from the same input, submit, onClick=?

    - is it a question of syntax?
    - is there a simple way around this?
    - is this simply not possible?

    Many thanks for your help.

    Happy Christmas
    Denis

  2. #2
    Join Date
    Dec 2002
    Posts
    83
    Denis,

    One option is to put your call to validate() as the first thing done in your WriteCustInfo() method. You're guaranteed that validation is done before you contiue with any processing. Maybe you want to return a String from WriteCustInfo which will either be an error message (from validate()) or a confirmation message.

    You might do this inside WriteCustInfo:


    var returnMessage = validate();

    if (returnMessage == "") {
    // all of the code to write cust info, set returnMessage
    }
    return

    As far as the basic concept of a Submit button triggering more than one method, I'd make a method called "mySubmitAction()" and just have that list the one or more method calls you want.


    -- Steven

  3. #3
    Join Date
    Dec 2002
    Posts
    8

    'A problem with syntax or something else ?'

    Steven,

    Thank you very much that is most helpful. The return string in WriteCustInfo you suggest works. Unfortunately, an alert relating to function CheckValidAmount() is now no longer functional.

    As an alternative, I attempted "mySubmitAction()". Due to very limited knowledge, I probably got the syntax all wrong/didn’t know where to put it etc? This is what I tried:

    <script language="JavaScript">
    function mySubmitAction()
    onClick="WriteCustInfo()"
    onClick="Validate()"
    }

    </script>

    What is the best way, please, of ensuring that both alerts function? It would also be helpful to know how to write “mySubmitAction()” correctly. Any explanations of errors would be particularly helpful, as I am an absolute beginner. Thanks for your patience.
    Very many thanks.

    Denis

  4. #4
    Join Date
    Dec 2002
    Posts
    83
    Denis,

    I was thinking of the Strings for display reasons, and didn't think about if you're using Alerts. Maybe making validate() return a boolean would be best. So in validate() you do your checks, if something is wrong then display an error Alert and return false, if all is OK then return true .

    So altering the part inside writeCustInfo():

    if (validate()) {
    // validate passed
    // all of the code to write cust info here
    }
    else {
    // validate failed, do any cleanup here (optional)
    }
    return

    So if validate() passes, you continue with the processing. If it fails (which means an error Alert was put up) you just return. I put the else {} there but that's only needed if you have some process you might want to do when validate() fails.

    I guess your checkValidAmount() would return boolean as well and you could set that call up the same was as validate(). So if it's called inside validate() (I'm guessing) then do something similar to the code above except with if (checkValidAmount()) {}

    I haven't done much Javascript lately so definately do whatever works for you.

    -- Steven

  5. #5
    Join Date
    Dec 2002
    Posts
    8

    Smile 'A problem with syntax or something else ?'

    Steve,

    Thanks for this - worked a treat! Your explanations were most helpful too!

    Happy Christmas,
    Denis

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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center