#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    21
    Rep Power
    0

    Validation JavaScript help.


    I am using this script to validate that a user inputs some data into certain fields.

    <!-- Begin
    function checkrequired(which) {
    var pass=true;
    if (document.images) {
    for (i=0;i<which.length;i++) {
    var tempobj=which.elements[i];
    if (tempobj.name.substring(0,9)=="missionId"||tempobj.name.substring(0,13)=="responderName") {
    if (((tempobj.type=="text"||tempobj.type=="textarea")&&
    tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
    tempobj.selectedIndex==0)) {
    pass=false;
    break;
    }
    }
    }
    }


    if (!pass) {
    alert("Please fill in the Mission ID and Responder fields.");
    return false;
    }
    else
    return true;
    }
    // End -->


    Currently, if a user does not enter data into one or both text fields, 'missionId' and 'respondername', the alert dialog asks to fill in the fields. This works fine, but it does not know what field has no data. For instance, if I left the missionId field blank, I would get the alert to "Please fill in the Mission ID and Responder fields.", but I should be only getting an alert saying to fill in the Mission ID field. In other words, I am looking to make the script check which field has no value and ask the user to enter a value for only that field.
    I know I am needing another if statement:
    if missionId == null, then alert("Please enter the Mission ID");
    and
    if respondername==null, alert("Please enter your name");

    just not sure how to do it.
    thanks in advance,
    m
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    147
    Rep Power
    12
    This should work for you:

    PHP Code:
    function validate_form(the_form) {
        for (
    0the_form.elements.length 1i++) {
            var 
    the_text the_form.elements[i].value;
            var 
    the_alt the_form.elements[i].alt;
            var 
    the_tag_name the_form.elements[i].tagName;
            if (
    the_text == '' && the_tag_name != 'SELECT') {
                
    alert('Please enter a ' the_alt '.');
                return 
    false;
            }
        }
        return 
    true;

    Just add an alt property to each of your form elements. Set the alt equal to whatever the alert box should ask for. Example:
    Code:
    <input type="text" name="fname" alt="first name">
    And you'll need this in your form element:
    Code:
    <form onsubmit="validate_form(this)">
    Note: the script ignores <select> elements b/c of a bug in IE, and b/c a select should always have a default value. It can be worked around, but it's probably more trouble than it's worth.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    21
    Rep Power
    0
    stone,

    how do I include two function calls in a single form element?

    here is what I am calling now:

    onSubmit="return checkrequired(this)"

    and I also need to call your function:

    onsubmit="validate_form(this)">



    thanks again, can't wait to try this out!
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    147
    Rep Power
    12
    It's easy, just combine them like this:

    Code:
    onsubmit="return checkrequired(this); validate_form(this)"
    You can execute as much javascript as you want in a single line as long as you seperate each statement with a semi-colon.

IMN logo majestic logo threadwatch logo seochat tools logo