I give every edit box an "id" that begins with "req" for REQUIRED, like id="reqFName" or id="reqLName", then I iterate through the Form collection of objects and any that begins with "req" is tested for its' condition. In the following, I predefine an array that has the id of the fields to iterate through instead

var arInpNames = [["reqfname","FIRST Name"],["reqlname","LAST Name"],["reqemail","EMAIL"]];

for (i=0; i < arInpNames.length; i++)
       {
         oEl = document.getElementById(arInpNames[i][0]);
         if (oEl.value.length===0)
           {
             alert(arInpNames[i][1] + " MUST be entered!!");
             oEl.focus();
             DataGood = false;
             break;
           }
       }

In the above instance, the array arInpNames contains the name value pairs of the fields and what they should contain, like:


so if "reqlname" doesn't have a value (its length is 0) then the alert contains the Name identifier that the user would see next to the field.

DataGood is a boolean that is tested after the exit of the iteration of the fields and if it is true, then it is in condition to be sent to the server.

----- Original Message ----- From: "Rachael Malberg" <[email protected]>
To: "RBASE-L Mailing List" <[email protected]>
Sent: Tuesday, March 16, 2010 5:01 PM
Subject: [RBASE-L] - RE: OT +x3 PHP


yeah PHP is server side scripting so the user has to send the data to the server and check it. While Javascript is client side so before they even send the data you can check it. here is my standard javascript validation code..
//new member form validation
function isEmptyNM()
{
if (document.memnew.fn.value == "" || document.memnew.fn.value ==null)
 {
 alert ("Please enter your first name.");
 document.memnew.fn.focus();
 return true;
 }
if (document.memnew.ln.value == "" || document.memnew.ln.value ==null)
 {
 alert ("Please enter your last name.");
 document.memnew.ln.focus();
 return true;
 }
if (document.memnew.email.value == "" || document.memnew.email.value ==null)
 {
 alert ("Please enter your email address.");
 document.memnew.email.focus();
 return true;
 }
if (document.memnew.pw.value == "" || document.memnew.pw.value ==null)
 {
 alert ("Please enter your password.");
 document.memnew.pw.focus();
 return true;
 }
if (document.memnew.pw2.value == "" || document.memnew.pw2.value ==null)
 {
 alert ("Please confirm your password.");
 document.memnew.pw2.focus();
 return true;
 }
if (document.memnew.pw2.value!=document.memnew.pw.value)
 {
 alert ("Your passwords do not match.");
 document.memnew.pw.focus();
 return true;
 }
else
{
return false;
}
}
//shipping address form validation
function isEmptySA()
{
if (document.memnew.OFN.value == "" || document.memnew.OFN.value ==null)
 {
 alert ("Please enter the ship to first name.");
 document.memnew.OFN.focus();
 return true;
 }
if (document.memnew.OLN.value == "" || document.memnew.OLN.value ==null)
 {
 alert ("Please enter  the ship to last name.");
 document.memnew.OLN.focus();
 return true;
 }
if (document.memnew.OPP1.value == "" || document.memnew.OPP1.value ==null)
 {
alert ("Please enter the ship to contact phone number area code. We need this in case of an issue.");
 document.memnew.OPP1.focus();
 return true;
 }
if (document.memnew.OPP2.value == "" || document.memnew.OPP2.value ==null)
 {
alert ("Please enter the ship to contact phone number. We need this in case of an issue.");
 document.memnew.OPP2.focus();
 return true;
 }
if (document.memnew.OPP3.value == "" || document.memnew.OPP3.value ==null)
 {
alert ("Please enter the ship to contact phone number. We need this in case of an issue.");
 document.memnew.OPP3.focus();
 return true;
 }
if (document.memnew.OSA1.value == "" || document.memnew.OSA1.value ==null)
 {
 alert ("Please enter the ship to address.");
 document.memnew.OSA1.focus();
 return true;
 }
if (document.memnew.OSC.value == "" || document.memnew.OSC.value ==null)
 {
 alert ("Please enter the ship to city.");
 document.memnew.OSC.focus();
 return true;
 }
if (document.memnew.OSZ.value == "" || document.memnew.OSZ.value ==null)
 {
 alert ("Please enter the ship to zip.");
 document.memnew.OSZ.focus();
 return true;
 }
else
{
return false;
}
}


function isEmptyBA()
{
if (document.memnew.NAME.value == "" || document.memnew.NAME.value ==null)
 {
 alert ("Please enter the billing name.");
 document.memnew.NAME.focus();
 return true;
 }
if (document.memnew.ADDRESS.value == "" || document.memnew.ADDRESS.value ==null)
 {
 alert ("Please enter the billing address.");
 document.memnew.ADDRESS.focus();
 return true;
 }
if (document.memnew.CITY.value == "" || document.memnew.CITY.value ==null)
 {
 alert ("Please enter the billing city.");
 document.memnew.CITY.focus();
 return true;
 }
if (document.memnew.ZIP.value == "" || document.memnew.ZIP.value ==null)
 {
 alert ("Please enter the billing zip.");
 document.memnew.ZIP.focus();
 return true;
 }
else
{
return false;
}
}


//make sure e-mail has an @ sign and a dot
function emailCheck()
{
if
(document.memnew.email.value.indexOf ('@',0) < 0 || document.memnew.email.value.indexOf ('.',0) < 0)
{
alert("Please enter a valid e-mail address.\nThe e-mail field requires an \"@\" and a \".\" be used.\n\nPlease re-enter your e-mail address.")
document.memnew.email.focus();
return true;
}
else
{
return false;
}
}
//allow valid characters - error if character not in this list
function emailChars()
{
var checkOK = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzfsosoyÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ[email protected]";
  var checkStr = document.memnew.email.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
alert("Please enter only letters, digits and \"@.-_\" characters in the \"email\" field.");
    document.memnew.email.focus();
    return true;
  }
}
//check for strings of @. and .. - error if found
function moreEmail()
{
if
(document.memnew.email.value.indexOf ('@.',0) > 0 || document.memnew.email.value.indexOf ('..',0) > 0)
{
alert("Please enter a valid e-mail address.")
document.memnew.email.focus();
return true;
}
else
{
return false;
}
}
function checkEmail()
{
var email = document.memnew.email.value
if (email.length  > 1)
 {
  if (emailCheck() || emailChars() || moreEmail())
   {
    return true;
   }

  else
 {
 return false;
 }
}
}


//Here is the batch form validation
function validateNMForm()
{

if (isEmptyNM())
 {
  return false;
 }
if (checkEmail())
 {
  return false;
 }
else
{
return true;
}
}

function validateSAForm()
{

if (isEmptySA())
 {
  return false;
 }
else
{
return true;
}
}
//validateBAForm
function validateBAForm()
{

if (isEmptyBA())
 {
  return false;
 }
if (checkEmail())
 {
  return false;
 }
else
{
return true;
}
}
function upperMe(formname,formfield)
{
 var field1 = eval( 'document.'+formname+'.'+formfield);
 s=field1.value.toUpperCase();
filteredValues = "^%_-|{}[]$&*()!,'<>?~`#";     // Characters stripped out
var i;
var returnString = "";
for (i = 0; i < s.length; i++) { // Search through string and append to unfiltered values to returnString.
var c = s.charAt(i);
if (filteredValues.indexOf(c) == -1) returnString += c;
}
 field1.value= returnString;
}

function openBrWindow(theURL,winName,features) {
 window.open(theURL,winName,features);
 }

 function NewWindow(mypage, myname, w, h, scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top=0,left='+winl+',scrollbars='+scroll+',resizable'
win = window.open(mypage, myname, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}
//

and here is my form (you'll have to strip out the PHP stuff)...
<form class="form" method="post" action="Order_4ProcShipInfo.php" name="memnew" onSubmit="return validateSAForm();" >
       <DIV class="formrow">
         <label>First Name:</label>
<input type="text" class="input" name="OFN" maxlength="20" onChange="upperMe('memnew','OFN');" value="<?php echo mysql_result($qr,0,"MemFN");?>">
       </DIV>
       <DIV class="formrow">
         <label>Last Name:</label>
<input type="text" class="input" name="OLN" maxlength="35" onChange="upperMe('memnew','OLN');" value="<?php echo mysql_result($qr,0,"MemLN");?>">
       </DIV>
       <DIV class="formrow">
         <label>Contact Phone:</label>
         (
<input type="text" class="input2" name="OPP1" maxlength="3" onChange="upperMe('memnew','OPP1');" value="<?php echo mysql_result($qr,0,"MemPP1");?>">
         )
<input type="text" class="input2" name="OPP2" maxlength="3" onChange="upperMe('memnew','OPP2');" value="<?php echo mysql_result($qr,0,"MemPP2");?>">
         -
<input type="text" class="input2" name="OPP3" maxlength="4" onChange="upperMe('memnew','OPP3');" value="<?php echo mysql_result($qr,0,"MemPP3");?>">
       </DIV>
       <DIV class="formrow">
         <label>Confirmation Email:</label>
<input type="text" class="input" name="email" maxlength="50" onChange="upperMe('memnew','email');" value="<?php echo mysql_result($qr,0,"MemEmail");?>">
       </DIV>
       <DIV class="formrow">
         <label>Address 1:</label>
<input type="text" class="input" name="OSA1" maxlength="35" onChange="upperMe('memnew','OSA1');" value="<?php echo mysql_result($qr,0,"MemSA1");?>">
       </DIV>
       <DIV class="formrow">
         <label>Address 2:</label>
<input type="text" class="input" name="OSA2" maxlength="35" onChange="upperMe('memnew','OSA2');" value="<?php echo mysql_result($qr,0,"MemSA2");?>">
       </DIV>
       <DIV class="formrow">
         <label>City:</label>
<input type="text" class="input" name="OSC" maxlength="35" onChange="upperMe('memnew','OSC');" value="<?php echo mysql_result($qr,0,"MemSC");?>">
       </div>
       <DIV class="formrow">
         <label> State:</label>
         <select name="OSS" class="input">
           <?php
    $i=0;
    while ($i<$stcnt){
     $StAbv=mysql_result($qsr,$i,"StAbv");
     $StName=mysql_result($qsr,$i,"StName");
     if($MemSS==$StAbv){
echo '<option value="'.$StAbv.'" selected>'.$StAbv.' - '.$StName.'</option>';
     } else {
echo '<option value="'.$StAbv.'" >'.$StAbv.' - '.$StName.'</option>';
     }
     $i++;
    }
   ?>
         </select>
       </div>
       <DIV class="formrow">
         <label> Zip:</label>
<input type="text" class="zipinput" name="OSZ" maxlength="10" onChange="upperMe('memnew','OSZ');" value="<?php echo mysql_result($qr,0,"MemSZ");?>">
       </div>
       <DIV class="formrow">
         <label> Country:</label>
         <select name="OSCtry" class="input">
           <?php
    $i=0;
    if($MemSCtry==null){
    $MemSCtry='US';
    }
    while ($i<$ctcnt){
     $countrysybl=mysql_result($qcr,$i,"countrysybl");
     $countryname=mysql_result($qcr,$i,"countryname");
     if($MemSCtry==$countrysybl){
echo '<option value="'.$countrysybl.'" selected>'.$countrysybl.' - '.$countryname.'</option>';
     } else {
echo '<option value="'.$countrysybl.'" >'.$countrysybl.' - '.$countryname.'</option>';
     }
     $i++;
    }
   ?>
         </select>
       </div>
<DIV class="formrowCntr"> Check here if billing address is the same:
         <input type="checkbox" name="BS" value="Y">
       </div>
       <DIV class="formrowRgt">
         <input type="submit" value="Continue > >" class="button">
       </DIV>
     </form>




----- Original Message ----- From: "MikeB" <[email protected]>
To: "RBASE-L Mailing List" <[email protected]>
Sent: Tuesday, March 16, 2010 3:51 PM
Subject: [RBASE-L] - RE: OT +x3 PHP


I expressed almost my entire knowledge of PHP in the last post...   ;-)

However, if you are doing validation on the client side, you wouldn't use PHP. You would use JScript or JavaScript.

 PHP can't know what you are doing at the client.
----- Original Message ----- From: "Paul InterlockInfo" <[email protected]>
To: "RBASE-L Mailing List" <[email protected]>
Sent: Tuesday, March 16, 2010 4:41 PM
Subject: [RBASE-L] - RE: OT +x3 PHP


Thank you Mike-

I was looking for a form like feel and make sure certain fields were filled or it would not send. So I need to check 'field1','field2','field3, etc... And make sure they have something in there. Do I just write the code as:

if(trim(field1)=="") or if(trim(field2) or if(trim(field3) or
if(trim(etc...) $validationOK=false;

The problem I have is the 20+ fields and wanting to make sure I have the
code right.




Sincerely,
Paul D.





-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of MikeB
Sent: Tuesday, March 16, 2010 4:20 PM
To: RBASE-L Mailing List
Subject: [RBASE-L] - RE: OT +x3 PHP

Both of the following are the same, that is why it said OR. I always use
the first in any language just to be clear at the expense of verbosity.

if(call()==TRUE)

if(call())

----- Original Message ----- From: "Paul InterlockInfo" <[email protected]>
To: "RBASE-L Mailing List" <[email protected]>
Sent: Tuesday, March 16, 2010 4:12 PM
Subject: [RBASE-L] - RE: OT +x3 PHP


Getting mixed input from the php manual and it is a rather simple (in
RBase)
script so:



if (Trim($EN6365)=="") $validationOK=false;



I would like to include all fields that are entered (no empty fields) so what is the separator for this? Is it as simple as the example below and
just use the 'or' even if this example shown had that commented out?





Sincerely,

Paul Dewey





Example:

<?php


if(call()==TRUE) // or if(call())
{
// nothing to do
}
else
{
// do something here
}
?>












Reply via email to