function problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dwayne Epps

    function problem

    This is probably a no brainer, but I am having no luck at getting this to
    work. I'm creating functions that will validate form data fields. For
    example, the following function:

    function checkText(field , msg, min, max) {
    if (!min) { min = 1 }
    if (!max) { max = 65535 }
    if (!field.value || field.value.len gth < min || field.value.max > max ||
    !field.value.ma tch(/^[a-zA-Z]*$/)) {
    alert(msg);
    field.focus();
    field.select();
    return false;
    }return true;
    }

    I tested the above function and it works great. The problem is I have
    several different functions that will check for fields with different data
    like: numbers only, selections in drop down menus, valid zip code, etc. I'm
    trying to create one function that I can call using the onSubmit event that
    will go through each function to validate all the fields in the form.

    I was trying to use the following function to accomplish this using just the
    first function. I would include other if statements for the other
    functions.

    function formValidation( thisform) {
    if (checkText(this .first_name, 'Please enter your first name.', 2,
    15)==false) {
    return true;
    }
    return false;
    }

    then within the form tag, I called the function: onSubmit="retur n
    formValidation( thisform)

    I'm not all that proficient with Javascript as I'm still learning. Can
    anyone identify my mistake in the formValidation function? The argument
    "thisform" doesn't need to be the name of the HTML form does it? I thought
    it could be any name of my choosing? Any help is greatly appreciated.
    Thanks in advance.
    -D-



  • Lee

    #2
    Re: function problem

    Dwayne said:
    [color=blue]
    >function formValidation( thisform) {
    >if (checkText(this .first_name, 'Please enter your first name.', 2,
    >15)==false) {
    >return true;
    >}
    >return false;
    >}
    >
    >then within the form tag, I called the function: onSubmit="retur n
    >formValidation (thisform)
    >
    >I'm not all that proficient with Javascript as I'm still learning. Can
    >anyone identify my mistake in the formValidation function? The argument
    >"thisform" doesn't need to be the name of the HTML form does it? I thought
    >it could be any name of my choosing? Any help is greatly appreciated.
    >Thanks in advance.[/color]

    In the line:
    function formValidation( thisform) {
    the "formal parameter" can be any name of your choosing,
    but you have to use it consistently. In the next line
    you refer to "this.first_nam e". That should be
    "thisform.first _name".

    In the onSubmit attribute, on the other hand, the "actual
    parameter" is not any name of your choosing. In this
    particular case, it should be "formValidation (this)",
    because the attribute named "this" is a reference to the
    form that is being submitted.

    Comment

    Working...