SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    more than 1 function call inside onSubmit clause

    Hi there, i have a HTML form in the onsubmit clause i would like to call a function.

    the function has a return value so must be called as:
    onsubmit ="return checkName('firstname', 'error'); "
    I need to call this function again inside the submit clause to test the surname.
    how do i have 2 calls to a function in the same onsubmit clause. what is the syntax for this, i have seen lots of examples on the internet but none of them work.

  2. #2
    SitePoint Evangelist bals28mjk's Avatar
    Join Date
    Aug 2007
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello lizzy123.

    If I were using that code, I would probably set up a function to validate both names e.g:
    Code:
    validateName(f,l,v)
    {
              if(checkName(f, v)&&checkName(l,v))
              {
                     return true;
              }
              else
              {
                     return false;
              }
    
    }
    
    onsubmit ="return validateName('firstname','surname', 'error'); "

  3. #3
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi, but i require this same function to validate for many other elements, thats why i did not hard code it, if i do it this way than i would have to do the same thing for every validation function i have for every element there is. do you know of any better way please?

  4. #4
    SitePoint Evangelist bals28mjk's Avatar
    Join Date
    Aug 2007
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't you have to make calls to all those functions anyway?
    Code JavaScript:
    if(
    checkName('firstname','error')&&
    checkName('lastname','error')&&
    validateAnother('n','v')&&
    validateYetAnother('n','v')
    )
    {return true;}
    else
    {return false}

  5. #5
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    .............
    Last edited by lizzy123; Dec 19, 2008 at 18:48.

  6. #6
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi bals28mjk, ok in a validation statment to get the code to work it can only have return false not return true because if it is not false then by default it is true.
    so the function checkName('firstname','error') returns false if not what it should be, so in the function to be called by onSubmit can only return false. but as all the function called in the new function are all already returning false. it would be correct to have code like

    function fname()
    {
    return (checkName('namep','error1')) && (checkName('namep2','error2'))

    }

    however when i do this the validation works for the first function called in the return of the fname function but the rest do not work.

    some help please!

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Code javascript:
    function fname() {
        var isValid = true;
        isValid = isValid && checkName('namep','error1');
        isValid = isValid && checkName('namep2','error2');
        return isValid;
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Evangelist bals28mjk's Avatar
    Join Date
    Aug 2007
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi bals28mjk, ok in a validation statment to get the code to work it can only have return false not return true because if it is not false then by default it is true.
    so the function checkName('firstname','error') returns false if not what it should be, so in the function to be called by onSubmit can only return false. but as all the function called in the new function are all already returning false. it would be correct to have code like

    function fname()
    {
    return (checkName('namep','error1')) && (checkName('namep2','error2'))

    }

    however when i do this the validation works for the first function called in the return of the fname function but the rest do not work.

    some help please!
    ¿Que?

    I don't follow lizzy123.

  9. #9
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok in a validation statment to get the code to work can only have return false not return true because if the condition is not false then by default it is true.
    so the function checkName('firstname','error') returns false if condition is false, so as all the functions called in the fname function are all already returning false. it would be correct to have code as:

    function fname()
    {
    return (checkName('namep','error1')) && (checkName('namep2','error2'))
    }

    however when i do this the validation works for the first checkname function called in the return of the fname function but the rest do not work.

    pmw57 i will try out the code you have posted, tnx

  10. #10
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi pmw57, i tried that out, it does not work. the error msg shows for the 1st text name in the function and does not show for the 2nd text name in the function. if the first field has correct input the form still gets submited even if 2nd field is empty or incorrect input.

  11. #11
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    The theory is solid, so let's see what's happening in practice.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  12. #12
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Perhaps then, and pardon me if this sounds obvious, the checkName function could return TRUE when it validates as correct.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  13. #13
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    This can all be cleared up if you provide the code that you're using.

    The statement that true is returned by default is a strong indication that help is definitely needed, because all javascript functions return undefined by default.

    So if you do want our help, please allow us to help you.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  14. #14
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your suggestion about having true is correct in the checkname function, i originally did have it before but than removed it when i was trying to call the checkname function in the onsubmit="", as it only worked by taking out the true statment.

  15. #15
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    It's lovely to hear that we were able to be of help for you here.

    Happy holidays and all the best for the new year.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  16. #16
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, have a nice xmas n happy new yr.


Bookmarks

Posting Permissions

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