Board index   FAQ   Search  
Register  Login
Board index php forum :: HTML JavaScript

Help with a form validation problem

Javascript coding ..

Moderators: macek, egami, gesf

Help with a form validation problem

Postby vorer » Fri Mar 03, 2006 5:36 am

I'll try to explain this the best I can.
I have about 42 PHP forms on our web site that are all validated using a validate.js script to ensure the required fields are populated. The top half of the form is the same for all forms so its included to the form when the user selects the specific form. What I discovered was almost all the forms have a Diagnosis text field however ther are about 10 forms that do not have this field because it's not required so when the js tries to validate the forms without the diagnosis text field it's stops validating that form. So I added this line into the js to see if it would help resolve the problem: No luck.

Code: Select all
if (_CF_this.DiagnosisText != null){
   if(_CF_this.DiagnosisText.value == ""){
      alert("Please enter a diagnosis.")
      return false;
   }
}

But it didn't resolve the problem. Below is the entire validation.js that I'm using. Any ideas what might help the problem? Thanks Rob
Code: Select all
function _CF_onError(form_object, input_object, object_value, error_message){
   alert(error_message);
          return false;
    }
function _CF_hasValue(obj, obj_type)
    {
    if (obj_type == "TEXT" || obj_type == "PASSWORD")
   {
       if (obj.value.length == 0)
            return false;
       else
            return true;
       }
    else if (obj_type == "SELECT")
   {
        for (i=0; i < obj.length; i++)
          {
      if (obj.options[i].selected)
         return true;
      }

          return false;   
   }
    else if (obj_type == "SINGLE_VALUE_RADIO" || obj_type == "SINGLE_VALUE_CHECKBOX")
   {

      if (obj.checked)
         return true;
      else
             return false;   
   }
    else if (obj_type == "RADIO" || obj_type == "CHECKBOX")
   {

        for (i=0; i < obj.length; i++)
          {
      if (obj[i].checked)
         return true;
      }

          return false;   
   }
   }

function _CF_checkdate(object_value)
    {
    //Returns true if value is a date format or is NULL
    //otherwise returns false   

    if (object_value.length == 0)
        return true;

    //Returns true if value is a date in the mm/dd/yyyy format
   isplit = object_value.indexOf('/');

   if (isplit == -1 || isplit == object_value.length)
      return false;

    sMonth = object_value.substring(0, isplit);

   if (sMonth.length == 0)
        return false;

   isplit = object_value.indexOf('/', isplit + 1);

   if (isplit == -1 || (isplit + 1 ) == object_value.length)
      return false;

    sDay = object_value.substring((sMonth.length + 1), isplit);

   if (sDay.length == 0)
        return false;

   sYear = object_value.substring(isplit + 1);

   if (!_CF_checkinteger(sMonth)) //check month
      return false;
   else
   if (!_CF_checkrange(sMonth, 1, 12)) //check month
      return false;
   else
   if (!_CF_checkinteger(sYear)) //check year
      return false;
   else
   if (!_CF_checkrange(sYear, 0, 9999)) //check year
      return false;
   else
   if (!_CF_checkinteger(sDay)) //check day
      return false;
   else
   if (!_CF_checkday(sYear, sMonth, sDay)) // check day
      return false;
   else
      return true;
    }



function _CF_checkday(checkYear, checkMonth, checkDay)
    {

   maxDay = 31;

   if (checkMonth == 4 || checkMonth == 6 ||
         checkMonth == 9 || checkMonth == 11)
      maxDay = 30;
   else
   if (checkMonth == 2)
   {
      if (checkYear % 4 > 0)
         maxDay =28;
      else
      if (checkYear % 100 == 0 && checkYear % 400 > 0)
         maxDay = 28;
      else
         maxDay = 29;
   }

   return _CF_checkrange(checkDay, 1, maxDay); //check day
    }



function _CF_checkinteger(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false   

    if (object_value.length == 0)
        return true;

    //Returns true if value is an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
   var decimal_format = ".";
   var check_char;

    //The first character can be + -  blank or a digit.
   check_char = object_value.indexOf(decimal_format)
    //Was it a decimal?
    if (check_char < 1)
   return _CF_checknumber(object_value);
    else
   return false;
    }



function _CF_numberrange(object_value, min_value, max_value)
    {
    // check minimum
    if (min_value != null)
   {
        if (object_value < min_value)
      return false;
   }

    // check maximum
    if (max_value != null)
   {
   if (object_value > max_value)
      return false;
   }
   
    //All tests passed, so...
    return true;
    }



function _CF_checknumber(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false   

    if (object_value.length == 0)
        return true;

    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
   var start_format = " .+-0123456789";
   var number_format = " .0123456789";
   var check_char;
   var decimal = false;
   var trailing_blank = false;
   var digits = false;

    //The first character can be + - .  blank or a digit.
   check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
   if (check_char == 1)
       decimal = true;
   else if (check_char < 1)
      return false;
       
   //Remaining characters can be only . or a digit, but only one decimal.
   for (var i = 1; i < object_value.length; i++)
   {
      check_char = number_format.indexOf(object_value.charAt(i))
      if (check_char < 0)
         return false;
      else if (check_char == 1)
      {
         if (decimal)      // Second decimal.
            return false;
         else
            decimal = true;
      }
      else if (check_char == 0)
      {
         if (decimal || digits)   
            trailing_blank = true;
        // ignore leading blanks

      }
           else if (trailing_blank)
         return false;
      else
         digits = true;
   }   
    //All tests passed, so...
    return true
    }



function _CF_checkrange(object_value, min_value, max_value)
    {
    //if value is in range then return true else return false

    if (object_value.length == 0)
        return true;


    if (!_CF_checknumber(object_value))
   {
   return false;
   }
    else
   {
   return (_CF_numberrange((eval(object_value)), min_value, max_value));
   }
   
    //All tests passed, so...
    return true;
    }



function _CF_checkphone(object_value)
    {
    if (object_value.length == 0)
        return true;
      
    if (object_value.length != 12)
        return false;

   // check if first 3 characters represent a valid area code
    if (!_CF_checknumber(object_value.substring(0,3)))
      return false;
    else
   if (!_CF_numberrange((eval(object_value.substring(0,3))), 100, 1000))
      return false;

   // check if area code/exchange separator is either a'-' or ' '
   if (object_value.charAt(3) != "-" && object_value.charAt(3) != " ")
        return false

   // check if  characters 5 - 7 represent a valid exchange
    if (!_CF_checknumber(object_value.substring(4,7)))
      return false;
    else
   if (!_CF_numberrange((eval(object_value.substring(4,7))), 100, 1000))
      return false;
   
   // check if exchange/number separator is either a'-' or ' '
   if (object_value.charAt(7) != "-" && object_value.charAt(7) != " ")
        return false;

   // make sure last for digits are a valid integer
   if (object_value.charAt(8) == "-" || object_value.charAt(8) == "+")
        return false;
   else
   {
      return (_CF_checkinteger(object_value.substring(8,12)));
   }
    }



function _CF_checkzip(object_value)
    {
    if (object_value.length == 0)
        return true;
      
    if (object_value.length != 5 && object_value.length != 10)
        return false;

   // make sure first 5 digits are a valid integer
   if (object_value.charAt(0) == "-" || object_value.charAt(0) == "+")
        return false;

   if (!_CF_checkinteger(object_value.substring(0,5)))
      return false;

   if (object_value.length == 5)
      return true;
   
   // make sure

   // check if separator is either a'-' or ' '
   if (object_value.charAt(5) != "-" && object_value.charAt(5) != " ")
        return false;

   // check if last 4 digits are a valid integer
   if (object_value.charAt(6) == "-" || object_value.charAt(6) == "+")
        return false;

   return (_CF_checkinteger(object_value.substring(6,10)));
    }


function  _CF_checkCFForm_1(_CF_this){
   if(_CF_this.ContactPerson.value == ""){
      alert("Please enter a contact person.")
      return false;
   }
   if(_CF_this.state.value == ""){
      alert("Please enter a state.")
      return false;
   }
   
if (_CF_this.DiagnosisText != null){
   if(_CF_this.DiagnosisText.value == ""){
      alert("Please enter a diagnosis.")
      return false;
   }
}


    if  (!_CF_checkphone(_CF_this.DirectPhone.value))

        {

        if  (!_CF_onError(_CF_this, _CF_this.DirectPhone, _CF_this.DirectPhone.value, "Please enter a valid direct telephone number."))

            {

            return false;

            }

        }


    if  (!_CF_hasValue(_CF_this.ReferringPhysician, "TEXT" ))

        {

        if  (!_CF_onError(_CF_this, _CF_this.ReferringPhysician, _CF_this.ReferringPhysician.value, "Please enter a referring physician."))

            {

            return false;

            }

        }


    if  (!_CF_hasValue(_CF_this.NurseLine, "TEXT" ))

        {

        if  (!_CF_onError(_CF_this, _CF_this.NurseLine, _CF_this.NurseLine.value, "Please enter a valid nurse line."))

            {

            return false;

            }

        }


    if  (!_CF_checkphone(_CF_this.NurseLine.value))

        {

        if  (!_CF_onError(_CF_this, _CF_this.NurseLine, _CF_this.NurseLine.value, "Please enter a valid nurse line."))

            {

            return false;

            }

        }


    if  (!_CF_hasValue(_CF_this.FaxNumber, "TEXT" ))

        {

        if  (!_CF_onError(_CF_this, _CF_this.FaxNumber, _CF_this.FaxNumber.value, "Please enter a valid fax number."))

            {

            return false;

            }

        }


    if  (!_CF_checkphone(_CF_this.FaxNumber.value))

        {

        if  (!_CF_onError(_CF_this, _CF_this.FaxNumber, _CF_this.FaxNumber.value, "Please enter a valid fax number."))

            {

            return false;

            }

        }


   if(!_CF_hasValue(_CF_this.PatientName, "TEXT" )){
      if (!_CF_onError(_CF_this, _CF_this.PatientName, _CF_this.PatientName.value, "Please enter a patient name.")){
         return false;
         }
   }


    if  (!_CF_hasValue(_CF_this.DOB, "TEXT" ))

        {

        if  (!_CF_onError(_CF_this, _CF_this.DOB, _CF_this.DOB.value, "Please enter the patient's date of birth."))

            {

            return false;

            }

        }


    if  (!_CF_checkdate(_CF_this.DOB.value))

        {

        if  (!_CF_onError(_CF_this, _CF_this.DOB, _CF_this.DOB.value, "Please enter the patient's date of birth."))

            {

            return false;

            }

        }


    if  (!_CF_hasValue(_CF_this.ParentName, "TEXT" ))

        {

        if  (!_CF_onError(_CF_this, _CF_this.ParentName, _CF_this.ParentName.value, "Please enter the partent or guardians name."))

            {

            return false;

            }

        }


    if  (!_CF_hasValue(_CF_this.MailingAddress, "TEXT" ))

        {

        if  (!_CF_onError(_CF_this, _CF_this.MailingAddress, _CF_this.MailingAddress.value, "Please enter a mailing address."))

            {

            return false;

            }

        }


    if  (!_CF_hasValue(_CF_this.City, "TEXT" ))

        {

        if  (!_CF_onError(_CF_this, _CF_this.City, _CF_this.City.value, "Please enter a city."))

            {

            return false;

            }

        }


    if  (!_CF_hasValue(_CF_this.Zip, "TEXT" ))

        {

        if  (!_CF_onError(_CF_this, _CF_this.Zip, _CF_this.Zip.value, "Please enter a ZIP code in xxxxx or xxxxx-xxxx format"))

            {

            return false;

            }

        }


    if  (!_CF_checkzip(_CF_this.Zip.value))

        {

        if  (!_CF_onError(_CF_this, _CF_this.Zip, _CF_this.Zip.value, "Please enter a ZIP code in xxxxx or xxxxx-xxxx format"))

            {

            return false;

            }

        }


    if  (!_CF_hasValue(_CF_this.HomePhone, "TEXT" ))

        {

        if  (!_CF_onError(_CF_this, _CF_this.HomePhone, _CF_this.HomePhone.value, "Please enter a valid home phone number."))

            {

            return false;

            }

        }


    if  (!_CF_checkphone(_CF_this.HomePhone.value))

        {

        if  (!_CF_onError(_CF_this, _CF_this.HomePhone, _CF_this.HomePhone.value, "Please enter a valid home phone number."))

            {

            return false;

            }

        }


    if  (!_CF_checkphone(_CF_this.MomDadWorkPhone.value))

        {

        if  (!_CF_onError(_CF_this, _CF_this.MomDadWorkPhone, _CF_this.MomDadWorkPhone.value, "Please enter a valid work phone number."))

            {

            return false;

            }

        }


    return true;

    }
vorer
New php-forum User
New php-forum User
 
Posts: 9
Joined: Fri Mar 03, 2006 5:14 am

Postby Coditor » Fri Mar 03, 2006 12:13 pm

Try this:

Code: Select all
if (_CF_this.DiagnosisText){
   if(_CF_this.DiagnosisText.value == ""){
      alert("Please enter a diagnosis.")
      return false;
   }
}


or shorter:
Code: Select all
if (_CF_this.DiagnosisText && _CF_this.DiagnosisText.value == ""){
   alert("Please enter a diagnosis.")
   return false;
}


Coditor
Coditor
New php-forum User
New php-forum User
 
Posts: 243
Joined: Wed Feb 01, 2006 9:18 am
Location: Netherlands

Thanks

Postby vorer » Mon Mar 06, 2006 4:35 am

Thanks for the reply. I tried both snipets of code but no luck. I wonder if there is something else a miss in the code itself.
vorer
New php-forum User
New php-forum User
 
Posts: 9
Joined: Fri Mar 03, 2006 5:14 am

Postby Coditor » Mon Mar 06, 2006 4:53 am

Do you get a javascript error? If so, what line?

Perhaps if you send us a URL where we can view this...

Coditor
Coditor
New php-forum User
New php-forum User
 
Posts: 243
Joined: Wed Feb 01, 2006 9:18 am
Location: Netherlands

Postby vorer » Mon Mar 06, 2006 5:03 am

No error messages.
The scritp passes the form to the next step (2 of 3) of the form process. You can see how this works form our web site. Go to https://www.childrensdayton.org/OnlineR ... _list1.php and select the Nutrition form from the list. If you hit submit you will see the script ask to fill in hte contact person then sit submit again, the script ask for state hit submit again and the form excepts the input and goes to step 2. As compared to the first form in the list "Adolescent Headache Clinic" it checks all the fields befor egoing to step 2.
vorer
New php-forum User
New php-forum User
 
Posts: 9
Joined: Fri Mar 03, 2006 5:14 am

Postby Coditor » Mon Mar 06, 2006 5:23 am

There is a javascript error on line 335: 'DiagnosisText.value' is null or not an object. If a Javascript error occurs, it doesn't continue to "return false" (or anything else) and thus the form is submitted.

Code: Select all
if(_CF_this.DiagnosisText.value == ""){
   alert("Please enter a diagnosis.")
   return false;
}


Should work fine if your replace it by:
Code: Select all
if(_CF_this.DiagnosisText && _CF_this.DiagnosisText.value == ""){
   alert("Please enter a diagnosis.")
   return false;
}


Coditor
Coditor
New php-forum User
New php-forum User
 
Posts: 243
Joined: Wed Feb 01, 2006 9:18 am
Location: Netherlands

darn

Postby vorer » Mon Mar 06, 2006 5:57 am

I gave the script a try on the developmet side and still passes the form to step 2. Any other ideas?

Thanks for all your help this morning. I can't wait to get this form resolved, its been a real pain for me.

Thanks,
Rob
vorer
New php-forum User
New php-forum User
 
Posts: 9
Joined: Fri Mar 03, 2006 5:14 am

I spoke to soon

Postby vorer » Mon Mar 06, 2006 6:21 am

Thank You the script seems to be working now!

Thank you so much for your help.

I truly appreciate it.

Thanks again,
Rob
:D
vorer
New php-forum User
New php-forum User
 
Posts: 9
Joined: Fri Mar 03, 2006 5:14 am

Postby Alexej Kubarev » Mon Mar 06, 2006 8:48 am

The post has been moved to a correct forum.
next time please use code tags when posting your code: it makes a bit easier to view
User avatar
Alexej Kubarev
Site Admin
Site Admin
 
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län


Return to JavaScript

Who is online

Users browsing this forum: No registered users and 0 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron