Hi everyone,

The following code has a javascript onblur event in the input fields so that if someone clicks out of the field without typing something in it, the original label, eg. 'First name' is reinserted into the field. The field is also being validated by some php code, however the onblur event creates a problem because the form still submits even if a name isn't typed into the field. It's assuming that 'First name' or 'Last name' are entries and therefore isn't validating it correctly.

        <div> 
          <label for="firstname">First Name:</label> 
          <input id="firstname" name="firstname" type="text" value="First name" onfocus="if (this.value == 'First name') this.value=''" onblur="if (this.value == '') this.value='First name'" />          
<?php if (ValidatedField('index_965','index_965')) { if ((strpos((",".ValidatedField("index_965","index_965").","), "," . "1" . ",") !== false || "1" == "")) { if (!(false)) { ?> <span style="color: red"> First name is required </span> <?php } } }?> </div> <div> <label for="lastname">Last Name:</label> <input id="lastname" name="lastname" type="text" value="Last name" onfocus="if (this.value == 'Last name') this.value=''" onblur="if (this.value == '') this.value='Last name'" /> </div> <?php if (ValidatedField('index_965','index_965')) { if ((strpos((",".ValidatedField("index_965","index_965").","), "," . "2" . ",") !== false || "2" == "")) { if (!(false)) { ?> <span style="color: red"> Last name is required </span> <?php } } }?> <div>

I'm guess it would have to check if the contents of the first field was 'First name' and if yes then validation would fail and so the span with the error text "First name is required" would show, then the same with the second field.

Any assistance with this, much appreciated.

    You could also add an onsubmit handler to your HTML form so that if fields contain their default values (e.g. "First Name"), then they are cleared before the form is submitted.

    Another alternative would be to simply insert text labels (e.g. "First Name: ") before the text fields and remove the values altogether.

    EDIT: Guess I should actually read all of the code before posting. :p

    Anyway, since you already have text labels for the fields, why have this default value as well? It's one thing to use Javascript to enhance the web experience, but at some point it just gets annoying and redundant.

      you would have to alter the ValidatedField() function so that it returns false on blank or "First name", "Last Name"

        Thanks for the replies,

        Sorry I should have removed those text labels - I'm not actually using them now.

        The following is the ValidatedField() function:

        <?php
        if (!session_id())  {
          session_start();
        }
        function ValidatedField($page,$field)  {
          $theFields= "";
          $retVal = "";
          if (isset($_SESSION["WAVT_".$page."_Errors"]))  {
            $theFields = "&".$_SESSION["WAVT_".$page."_Errors"];
          }
          if (strpos($theFields,"&WAVT_".$field."=") !== false)  {
            $retVal = substr($theFields,strpos($theFields,"&WAVT_".$field."=")+strlen("&WAVT_".$field."="));
          }
          if (strpos($retVal,"&WAVT_") !== false)  {
            $retVal = substr($retVal,0,strpos($retVal,"&WAVT_"));
          }
          if ($retVal == "" && $page == $field) {
            $retVal = ValidatedField($page,$field."_Errors");
          }
          return $retVal;
        }
        
        ?>

        Can I get some help revising this to suit?

          Maybe I am missing something, but I think we need to see more code here. ValidatedField() is using WAVT, where does that come in? Further, the two arguments in the function are $page and $field, so why in your use of the function are both being passed through with the same value, and further to that where are you passing the field name?

          At any rate... Seeing as the addition and removal of last name will only work using JS why not use an onsubmit as brad said, that would probably be the easiest route. Failing that... depending on how many fields you have why not write a validation script from scratch?

            Write a Reply...