#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Posts
    5
    Rep Power
    0

    Question


    I'm just starting to write forms validation using jscript, and have done well so far. However, I have coded a function that checks most of the form fields (except radios) and alerts the user when I click the submit button. However, the focus remains on the submit button leaving the possibility for the user to submit the form anyway. How can I change the focus from the submit button to another object within the function?
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Posts
    5
    Rep Power
    0
    It could have something to do with the fact that I can't get my code to compile past the for statement.

    function FormValCheck()
    {
    moveto = "";
    for(i=0;i<89;i++)
    {
    valU = document.forms(0).elements(i).value;
    thing = document.forms(0).elements(i).name;
    if(valU == "")
    {
    alert("Invalid Entry! Check: " + thing);
    }
    }
    if(i < 90)
    {
    alert("!!!");
    }
    }
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Posts
    5
    Rep Power
    0
    Is there a way to keep a form from submitting even if the user presses enter within the form?

    Can an object be removed from the tab order?
  6. #4
  7. T-Shirt Tragic
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2001
    Location
    Melbourne, Australia
    Posts
    886
    Rep Power
    321
    not sure if you can remove an object from the tab order but you could take focus off an object if it becomes focused :-
    onFocus="this.blur()"
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Posts
    5
    Rep Power
    0

    Removing and Object from Tab Order


    Thanks Jonathon, I've figured out how to remove object from taborder. Just set <... tabindex="-1"). I just tried it and it worked!

    However, I can't seem to get your idea to work for the submit button. No matter what I seem to do, I can hit Enter and the damn form submits anyway. I always loved that with IE, but now it is biting at my heels!
  10. #6
  11. T-Shirt Tragic
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2001
    Location
    Melbourne, Australia
    Posts
    886
    Rep Power
    321
    One way would be to disable the submit button "<input type=submit name=submit value=submit disabled>" and validate the form fields as the user fills them out using the onchange or onblur events. When the user gets to the last form field and has correctly filled out all other fields you can enable the submit button "document.forms[0].submit.disabled=false" and the user will be able to submit the form.
    If the user incorrectly fills out a field and tries to tab to the next field an alert can appear telling the user what the problem is... from a usability point of view this could be better than validating the form all in one go after the user has been over the whole thing and tried to submit it, might be friendlier to help the user fix incorrect entries as they happen.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    81
    Rep Power
    14
    It sounds like you are attaching the validation scripts to each of the form elements. Why not just attach one to the main form tag? Then in your validation function return false if any of the validation fails -- this will prevent the form from submitting no matter what. Example,

    <FORM onsubmit="return FormValidator(this)" >....
    Robert Dominy
    About Guide for JavaScript
    http://javascript.about.com
    Software Consulting & Development
    http://www.angusog.com

IMN logo majestic logo threadwatch logo seochat tools logo