SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    960
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Whats Wrong with this Function

    Hai, whats wrong with this function.

    Code:
    function validate_fields(){ 
    if (feedback.txtname.value==""){
    alert("Name is not Optional. Please Enter Your Name.");
    feedback.txtname.focus(); 
    return false; 
    }elseif (feedback.txtemail.value==""){
    alert("Please Enter Your Email Address.");
    feedback.txtemail.focus(); 
    return false; 
    }elseif (feedback.txtcomments.value==""){
    alert("Please type your message.");
    feedback.txtcomments.focus(); 
    return false; 
    }else{
    }
    }
    Code:
    ... onSubmit="return validate_fields(feedback)....
    If i use only the following part. it works very well. Great Code !!
    but if i put other elaseif parts,

    Code:
    if (feedback.txtname.value==""){
    alert("Name is not Optional. Please Enter Your Name.");
    feedback.txtname.focus(); 
    return false; 
    }

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    because your function() doesn't contain a parameter to pass across.

    change function validate_fields() to function validate_fields(feedback)


  3. #3
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    960
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks gRoberts for pointing out the problem.

    I just put (feedback) as you sad.
    but still problem is there.

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    by the looks of it your passing the form across.

    Your best off using document.getElementById('fieldname').value


  5. #5
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    960
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    ok.ill check

  6. #6
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    960
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    gRobert and all

    The real problem with my above code is , if i put elseif part it will not work.
    but only the below part, works very well.

    if (feedback.txtname.value==""){
    alert("Name is not Optional. Please Enter Your Name.");
    feedback.txtname.focus();
    return false;
    }


    any problem with if ... else

  7. #7
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok...

    well if the txtname.value is empty, then the validate_fields will end, as you have return false. This means once it hits return false, it skips whats yet to come.


  8. #8
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    960
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ok gRob. but problem is, nothing happen when there is elseif added. even if the txtname is empty, it wan't alert.

  9. #9
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    960
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    if i remove the bleow part it works.

    elseif (feedback.txtemail.value==""){
    alert("Please Enter Your Email Address.");
    feedback.txtemail.focus();
    return false;
    }elseif (feedback.txtcomments.value==""){
    alert("Please type your message.");
    feedback.txtcomments.focus();
    return false;
    }else{
    }

  10. #10
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    960
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    oh, it was problem with 'elseif' and it should be 'else if' in java script. i come from php and did not notice this issue.

    Thanks gRob for the support. the parameter issue you pointed out is also a mistake i made.

    thanks.


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
  •