YEAR field regular expresion validations

Discussion in 'PHP' started by babyphp, Jun 5, 2008.

  1. #1
    i need a regular expression for just year like 1988.....

    this is the year code


    <
    select name="YearEstablished">
    <option value="">yyyy</option>
    <?php
    for ($i=2005; $i>=1900; $i--)
    {
    echo "<option value='$i'";
    if ($fields["YearEstablished"] == $i)
    echo " selected";
    echo ">$i</option>";
    }
    ?>
    </select>

    here is the regular expressions am using

    $rules[] = "required,YearEstablished,Please enter a xxxxxxxxxxxxxxxxxx.";
    $rules[] = "reg_exp,YearEstablished,^[0-9{4}],YearEstablished: XXXXXXXXX (Valid: a-z 0-9 space and dash.)";



    here is my error

    YearEstablished: Invalid charachters detected. (Valid: a-z 0-9 space and dash.)
     
    babyphp, Jun 5, 2008 IP
  2. JavaPF

    JavaPF Member

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #2
    That reg ex looks OK to me. Are you actually sending the value 1988 or yyyy?

    It looks to me that you would get that output error after sending yyyy because the reg ex is looking for numbers only.
     
    JavaPF, Jun 5, 2008 IP
  3. babyphp

    babyphp Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    when i sedn yyyy i get
    Please enter a xxxxxxxxxxxxxxxxxx.

    but when i pick a number from the drop down menu i get this
    YearEstablished: Invalid charachters detected. (Valid: a-z 0-9 space and dash.)
     
    babyphp, Jun 5, 2008 IP
  4. babyphp

    babyphp Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    <?php
    require ("dbconnection.inc.php");
    connect();
    $nextpage = "input-step2.php";
    $errormsg = '';

    $errors = array(); // set the errors array to empty, by default
    $fields = array(); // stores the field values
    $success_message = "";

    // re-populate the form fields
    $fields = $_POST;

    if (isset($_POST['submit'])) {
    // import the validation library
    require ("validation.php");
    $rules = array(); // stores the validation rules

    // standard form fields
    $rules[] = "required,CompanyName,Please enter a CompanyName.";
    $rules[] = "reg_exp,CompanyName,^[A-za-z0-9 -]+$,CompanyName: Invalid charachters detected. (Valid: a-z 0-9 space and dash.)";

    $rules[] = "required,TradingName,Please enter a TradingName.";
    $rules[] = "letters_only,TradingName,TradingName: only enter letters (a-Z) in this field.";

    // Numbers / alphanumeric fields
    $rules[] = "required,NumberOfSites,Please enter a NumberOfSites.";
    $rules[] = "digits_only,NumberOfSites,NumberOfSites may only contain digits.";

    $rules[] = "required,NumberOfEmployees,Please enter a NumberOfEmployees.";
    $rules[] = "digits_only,NumberOfEmployees,NumberOfEmployees may only contain digits.";

    $rules[] = "required,CompanyVATNumber,Please enter a CompanyVATNumber.";
    $rules[] = "digits_only,CompanyVATNumber,CompanyVATNumber may only contain digits.";

    $rules[] = "required,CompanyRegistrationNumber,Please enter a CompanyRegistrationNumber.";
    $rules[] = "digits_only,CompanyRegistrationNumber,CompanyRegistrationNumber may only contain digits.";





    $rules[] = "required,YearEstablished,Please enter a xxxxxxxxxxxxxxxxxx.";
    $rules[] = "reg_exp,YearEstablished,[0-9]{4}],YearEstablished: Invalid charachters detected. (Valid: a-z 0-9 space and dash.)";




    $rules[] = "required,HouseNameNumber,Please enter a HouseNameNumber.";
    //$rules[] = "digits_only,HouseNameNumber,HouseNameNumber may only contain digits.";
    $rules[] = "reg_exp,HouseNameNumber,^[A-za-z0-9 -]+$,HouseNameNumber: Invalid charachters detected. (Valid: a-z 0-9 space and dash.)";

    $rules[] = "required,NatureOfBusiness,Please enter a NatureOfBusiness.";
    $rules[] = "letters_only,NatureOfBusiness,NatureOfBusiness: only enter letters (a-Z) in this field.";

    $rules[] = "required,AddressLine1,Please enter an AddressLine1.";
    $rules[] = "letters_only,AddressLine1,AddressLine1: only enter letters (a-Z) in this field.";

    // $rules[] = "required,AddressLine2,Please enter an AddressLine2.";
    $rules[] = "letters_only,AddressLine2,AddressLine2: only enter letters (a-Z) in this field.";

    $rules[] = "required,TownCity,Please enter a TownCity.";
    $rules[] = "letters_only,TownCity,TownCity: only enter letters (a-Z) in this field.";

    $rules[] = "required,Country,Please enter a County.";
    $rules[] = "letters_only,Country,Country: only enter letters (a-Z) in this field.";

    // TODO: fix this...
    #$rules[] = "custom_alpha,PostCode,LxxxxL,PostCode: Please enter POST CODE IN FORM OF me12jr.";
    #$rules[] = "required,custom_alpha_field1,Please enter a PostCode.";

    $fields['PostCode'] = strtoupper($fields['PostCode']);
    $rules[] = "required,PostCode,Please enter a PostCode.";
    $rules[] = "reg_exp,PostCode,(((^[BEGLMNS][1-9]\d?)|(^W[2-9])|(^(A[BL]|B[ABDHLNRST]|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]|F[KY]|G[LUY]|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]|M[EKL]|N[EGNPRW]|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKL-PRSTWY]|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)\d\d?)|(^W1[A-HJKSTUW0-9])|(((^WC[1-2])|(^EC[1-4])|(^SW1))[ABEHMNPRVWXY]))(\s*)?([0-9][ABD-HJLNP-UW-Z]{2}))$|(^GIR\s?0AA$),PostCode: Invalid postcode detected. (Valid: ME10 4JR)";

    $errors = validateFields($fields, $rules);

    if (empty($errors)) {
    // no errors! redirect the user to the thankyou page (or whatever)
    $message = "All fields have been validated successfully!";

    mysql_query("update Counters set Value=Value+1 where Description = 'AddressID' limit 1;");
    $AddressID_result_set = mysql_query("select Value from Counters where Description = 'AddressID' limit 1;");
    $AddressID_result_row = mysql_fetch_array($AddressID_result_set);
    $fields['AddressID'] = $AddressID_result_row[0];

    $query = "insert into Addresses (AddressID, HouseNameNumber, AddressLine1, AddressLine2, TownCity, PostCode, Country) values ('".$fields['AddressID']."', '".$fields['HouseNameNumber']."', '".$fields['AddressLine1']."', '".$fields['AddressLine2']."', '".$fields['TownCity']."', '".$fields['PostCode']."', '".$fields['Country']."');";
    $result = mysql_query($query);

    if (!$result) { echo "<b>ERROR: ".mysql_error()."</b><br>Query: \"".$query."\""; exit; }

    mysql_query("update Counters set Value=Value+1 where Description = 'BusinessID' limit 1;");
    $BusinessID_result_set = mysql_query("select Value from Counters where Description = 'BusinessID' limit 1;");
    $BusinessID_result_row = mysql_fetch_array($BusinessID_result_set);
    $fields['BusinessID'] = $BusinessID_result_row[0];

    $query = "insert into Businesses (BusinessID, CompanyName, TradingName, BusinessTypeID, NumberOfSites, NumberofEmployees, CompanyVATNumber, CompanyRegistrationNumber, RegisteredAddressID, YearEstablished, NatureOfBusiness) values ('".$fields['BusinessID']."', '".$fields['CompanyName']."', '".$fields['TradingName']."', '".$fields['BusinessTypeID']."', '".$fields['NumberOfSites']."', '".$fields['NumberofEmployees']."', '".$fields['CompanyVATNumber']."', '".$fields['CompanyRegistrationNumber']."', '".$fields['AddressID']."', '".$fields['YearEstablished']."', '".$fields['NatureOfBusiness']."')";
    $result = mysql_query($query);

    if (!$result) { echo "<b>ERROR: ".mysql_error()."</b><br>Query: \"".$query."\""; exit; }

    // redirect user to next page...
    $nextpage.= "?BusinessID=".$fields['BusinessID']."";
    #header("Location: $nextpage");
    echo "Success! Please go to the <a href=\"$nextpage\">next page</a>.";
    exit;
    } // end if errors

    } // end if submitting

    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <link href="vodacom.css" rel="stylesheet" type="text/css" />
    <title>Voda Communications - New Customer</title>
    </head>

    <body>
    <table width="849" border="0" cellpadding="0" cellspacing="0">
    <tr>
    <td width="849">
    <table width="96%" border="0" align="left" cellpadding="0" cellspacing="0" class="hr">
    <tr>
    <th>
    <h2 align="center"><img src="Vodacom_logo.jpg" alt="" width="123" height="85" /></h2>

    <h2 align="center">vodacom customer relation management</h2> </th>
    </tr>
    </table>
    </td>
    </tr>
    </table>

    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">

    <h2>Contact Business</h2>
    <?php
    // if $errors is not empty, the form must have failed one or more validation
    // tests. Loop through each and display them on the page for the user
    if (!empty($errors)) {
    echo "<div class='error'>Please fix the following errors:\n<ul>";
    foreach($errors as $error) echo "<li>$error</li>\n";
    echo "</ul></div>";
    }
    if (!empty($message)) {
    echo "<div class='notify'>$success_message</div>";
    }
    ?>
    <table border="0" cellspacing="1" cellpadding="5">
    <tr>
    <th class="hr"><div align="left">Company Name</div></th>

    <td><input type="text" name="CompanyName" maxlength="45" value="<?=$fields['CompanyName'] ?>" /></td>
    </tr>

    <tr>
    <th class="hr"><div align="left">Trading Name</div></th>

    <td><input type="text" name="TradingName" maxlength="45" value="<?=$fields['TradingName'] ?>" /></td>
    </tr>

    <tr>
    <th class="hr"><div align="left">Business Type</div></th>

    <td><?php
    $result = mysql_query("SELECT BusinessTypeID, Description FROM BusinessTypes");
    echo "<select name=\"BusinessTypeID\">";
    while ($row = mysql_fetch_array($result)) {
    $id = $row['BusinessTypeID'];
    $description = $row['Description'];
    $on = ($BusinessTypeID == $id) ? ' selected="selected"' : '';
    echo "<option value=\"$id\"$on>$description</option>";
    }
    echo "</select>";
    ?></td>
    </tr>

    <tr>
    <th class="hr"><div align="left">Number Of Sites</div></th>

    <td><input type="text" name="NumberOfSites" value="<?=$fields['NumberOfSites'] ?>" /></td>
    </tr>

    <tr>
    <th class="hr"><div align="left">Number Of Employees</div></th>

    <td><input type="text" name="NumberOfEmployees" value="<?=$fields['NumberOfEmployees'] ?>" /></td>
    </tr>

    <tr>
    <th class="hr"><div align="left">Company VAT Number</div></th>

    <td><input type="text" name="CompanyVATNumber" value="<?=$fields['CompanyVATNumber'] ?>" /></td>
    </tr>

    <tr>
    <th class="hr"><div align="left">Company Registration Number</div></th>

    <td><input type="text" name="CompanyRegistrationNumber" value="<?=$fields['CompanyRegistrationNumber'] ?>" /></td>
    </tr>

    <tr>
    <th class="hr"><div align="left">Year Established</div></th>

    <td><select name="YearEstablished">
    <option value="">yyyy</option>
    <?php
    for ($i=2005; $i>=1900; $i--)
    {
    echo "<option value='$i'";
    if ($fields["YearEstablished"] == $i)
    echo " selected";
    echo ">$i</option>";
    }
    ?>
    </select></td>
    </tr>

    <tr>
    <th class="hr"><div align="left">Nature Of Business</div></th>

    <td><input type="text" name="NatureOfBusiness" maxlength="45" value="<?=$fields['NatureOfBusiness'] ?>" /></td>
    </tr>
    </table>

    <h2>Registered Address</h2>

    <table border="0" cellspacing="1" cellpadding="5">
    <tr>
    <th class="hr"><div align="left">HouseNameNumber</div></th>

    <td><input type="text" name="HouseNameNumber" maxlength="45" value="<?=$fields['HouseNameNumber'] ?>" /></td>
    </tr>

    <tr>
    <th class="hr"><div align="left">AddressLine1</div></th>

    <td><input type="text" name="AddressLine1" maxlength="45" value="<?=$fields['AddressLine1'] ?>" /></td>
    </tr>

    <tr>
    <th class="hr"><div align="left">AddressLine2</div></th>

    <td><input type="text" name="AddressLine2" maxlength="45" value="<?=$fields['AddressLine2'] ?>" /></td>
    </tr>

    <tr>
    <th class="hr"><div align="left">TownCity</div></th>

    <td><input type="text" name="TownCity" maxlength="45" value="<?=$fields['TownCity'] ?>" /></td>
    </tr>

    <tr>
    <th class="hr"><div align="left">PostCode</div></th>

    <td><input type="text" name="PostCode" maxlength="45" value="<?=$fields['PostCode'] ?>" /></td>
    </tr>

    <tr>
    <th class="hr"><div align="left">County</div></th>

    <td><input type="text" name="Country" maxlength="45" value="<?=$fields['Country'] ?>" /></td>
    </tr>
    </table>

    <p>&nbsp;</p>

    <p><input type="submit" name="submit" value="Submit" /></p>
    </form>
    </body>
    </html>
     
    babyphp, Jun 5, 2008 IP
  5. Lordo

    Lordo Well-Known Member

    Messages:
    2,082
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    190
    #5
    The rule does not look OK. The number of occurences should be outside the brackets:

    ^[0-9]{4}
     
    Lordo, Jun 5, 2008 IP
  6. babyphp

    babyphp Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    lordo you are a star if you was a girl i will give you kisses lol!!!
    that woked perfectly fine

    thanks to javapf as well
     
    babyphp, Jun 5, 2008 IP
  7. JavaPF

    JavaPF Member

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    ah well spotted.
     
    JavaPF, Jun 5, 2008 IP
  8. Lordo

    Lordo Well-Known Member

    Messages:
    2,082
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    190
    #8
    A pleasure :)

    (not talking about the kiss)
     
    Lordo, Jun 5, 2008 IP